plt.plot 用dataframe的一列
时间: 2023-07-23 19:43:16 浏览: 135
可以使用 `df['column_name'].plot()` 来绘制dataframe中的一列数据。例如,如果你的dataframe名为 `df`,并且你想绘制名为 `column1` 的列,你可以使用以下代码:
```
import pandas as pd
import matplotlib.pyplot as plt
# 读取数据
df = pd.read_csv('your_data.csv')
# 绘制图形
df['column1'].plot()
# 显示图形
plt.show()
```
这将绘制 `column1` 列的所有值,并在图形中显示。你可以根据需要添加其他参数来自定义图形。
相关问题
dataframe传入plt.plot
要将 DataFrame 传递给 `plt.plot()`,需要指定 X 轴和 Y 轴的列。例如,假设 DataFrame 中有两列数据 `x` 和 `y`,可以使用如下代码绘制它们的折线图:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 创建 DataFrame
df = pd.DataFrame({'x': [1, 2, 3, 4, 5], 'y': [2, 4, 6, 8, 10]})
# 绘制折线图
plt.plot(df['x'], df['y'])
# 显示图形
plt.show()
```
这将创建一个简单的折线图,其中 X 轴为 `x` 列,Y 轴为 `y` 列。可以根据需要修改列名。
修改代码错误: plt.plot(pre_array, 'g') plt.plot(test_labels, "r") df = pd.DataFrame({'pre_array': pre_array, 'test_labels': test_labels}) sns.lineplot(data=df, x="pre_array", y="test_labels", hue="event") plt.title('LSTM test mae: ' + str(loss_mae.item())) plt.savefig("lstm_test.png") plt.show()
根据您提供的代码,似乎没有定义 "event"。因此,您需要先定义 "event" 然后再使用它来绘制线图。
下面是修改后的代码:
```python
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# define event
event = ["event"] * len(pre_array)
# create dataframe
df = pd.DataFrame({'pre_array': pre_array, 'test_labels': test_labels, 'event': event})
# plot the data
plt.plot(pre_array, 'g')
plt.plot(test_labels, "r")
sns.lineplot(data=df, x="pre_array", y="test_labels", hue="event")
plt.title('LSTM test mae: ' + str(loss_mae.item()))
plt.savefig("lstm_test.png")
plt.show()
```
首先,我们定义了一个名为 "event" 的变量,它是一个包含 "event" 字符串的列表,长度与 "pre_array" 和 "test_labels" 相同。然后,我们使用这些变量定义了一个 Pandas DataFrame。最后,我们使用 Seaborn 绘制了带有 "event" 颜色编码的折线图。
阅读全文
相关推荐
















