Python 使用数据框以不同颜色绘制多条线
我有一个如下所示的数据帧Python 使用数据框以不同颜色绘制多条线,python,pandas,plot,Python,Pandas,Plot,我有一个如下所示的数据帧 color x y 0 red 0 0 1 red 1 1 2 red 2 2 3 red 3 3 4 red 4 4 5 red 5 5 6 red 6 6 7 red 7 7 8 red 8 8 9 red 9 9 10 blue 0 0 11 blue 1 1 12 blue 2 4 13 blue 3
color x y
0 red 0 0
1 red 1 1
2 red 2 2
3 red 3 3
4 red 4 4
5 red 5 5
6 red 6 6
7 red 7 7
8 red 8 8
9 red 9 9
10 blue 0 0
11 blue 1 1
12 blue 2 4
13 blue 3 9
14 blue 4 16
15 blue 5 25
16 blue 6 36
17 blue 7 49
18 blue 8 64
19 blue 9 81
我最终想要两条线,一条蓝,一条红。红线基本上应该是y=x,蓝线应该是y=x^2
当我执行以下操作时:
df.plot(x='x', y='y')
输出如下:
有没有办法让熊猫知道有两套呢?并对其进行相应的分组。我希望能够将列
color
指定为设置区分符您可以使用groupby
根据颜色将数据帧拆分为子组:
for key, grp in df.groupby(['color']):
屈服
您可以使用此代码获得所需的输出
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'color': ['red','red','red','blue','blue','blue'], 'x': [0,1,2,3,4,5],'y': [0,1,2,9,16,25]})
print df
color x y
0 red 0 0
1 red 1 1
2 red 2 2
3 blue 3 9
4 blue 4 16
5 blue 5 25
作图
a = df.iloc[[i for i in xrange(0,len(df)) if df['x'][i]==df['y'][i]]].plot(x='x',y='y',color = 'red')
df.iloc[[i for i in xrange(0,len(df)) if df['y'][i]== df['x'][i]**2]].plot(x='x',y='y',color = 'blue',ax=a)
plt.show()
输出
另一种简单的方法是使用
pivot
功能,根据需要先格式化数据
df.plot()
df = pd.DataFrame([
['red', 0, 0],
['red', 1, 1],
['red', 2, 2],
['red', 3, 3],
['red', 4, 4],
['red', 5, 5],
['red', 6, 6],
['red', 7, 7],
['red', 8, 8],
['red', 9, 9],
['blue', 0, 0],
['blue', 1, 1],
['blue', 2, 4],
['blue', 3, 9],
['blue', 4, 16],
['blue', 5, 25],
['blue', 6, 36],
['blue', 7, 49],
['blue', 8, 64],
['blue', 9, 81],
], columns=['color', 'x', 'y'])
df = df.pivot(index='x', columns='color', values='y')
df.plot()
pivot有效地将数据转换为:
如果您安装了seaborn
,则无需执行pivot
,这是一种更简单的方法:
import seaborn as sns
sns.lineplot(data=df, x='x', y='y', hue='color')
ax=grp.plot(ax=ax,kind='line',x='x',y='y',c=key,label=key)
添加“label”kwarg可以避免您事后操纵图例。@ErnestScribbler:谢谢您的改进!无法使解决方案工作,因为我的数据集不是颜色,而参数“c”恰好是打印线颜色。在OP的情况下,它是好的,但它将失败的其他人。如果您删除该参数,此奇妙的解决方案也将适用于所有其他数据集。如果两个(或N个)数据集的x轴值不相同,则此解决方案比公认的解决方案效果更好。使用pd.crosstab时,此解决方案更为优雅。更为简单,此解决方案非常有效,谢谢。
import seaborn as sns
sns.lineplot(data=df, x='x', y='y', hue='color')