首页 > 解决方案 > Matplotlib:如何在 matplotlib 上使用列作为数据分割?

问题描述

我有一个关于 matplotlib 的问题。

我有一张这样的桌子。

data = pd.Dataframe(alldata)

print(data)


index | id |  type   |  date   | value |
---------------------------------------
0   | 1  | apples   | 01-01-15| 100.00|
0   | 1  | apples   | 01-01-16| 100.10|
0   | 1  | apples   | 01-01-17| 100.15|
0   | 2  | bananas  | 01-01-15| 100.00|
0   | 2  | bananas  | 01-01-16| 100.05|
0   | 2  | bananas  | 01-01-17| 100.06|
0   | 3  | grapes   | 01-01-15| 100.00|
0   | 3  | grapes   | 01-01-16| 100.20|
0   | 3  | grapes   | 01-01-17| 100.40|

我的意图是在 x = 日期和 y = 值上绘制折线图,​​而是只创建一条线。我想创建 3 条线,使用“类型”作为参数,将一条线与另一条线进行比较。

我试图划分我的数据,如下所示:

x = data['data']
y1 = data['value'][data.type == 'apples']
y2 = data['value'][data.type == 'bananas']

并创建一个情节,如..

plt.plot(x,y1)
plt.plot(x,y2)

但它没有奏效。

如何在 matplotlib 上对特定参数使用数据分割?

多谢!

标签: pythonpandasmatplotliblinechartdata-segment

解决方案


推荐阅读