Python 用熊猫数据框绘制多条线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29233283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:15:20  来源:igfitidea点击:

Plotting multiple lines with pandas dataframe

pythonpandasplot

提问by sedavidw

I have a dataframe that looks like the following

我有一个如下所示的数据框

   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

I ultimately want two lines, one blue, one red. The red line should essentially be y=x and the blue line should be y=x^2

我最终想要两条线,一条蓝色,一条红色。红线本质上应该是 y=x,蓝线应该是 y=x^2

When I do the following:

当我执行以下操作时:

df.plot(x='x', y='y')

The output is this:

输出是这样的:

Is there a way to make pandas know that there are two sets? And group them accordingly. I'd like to be able to specify the column 'color' as the set differentiator

有没有办法让熊猫知道有两组?并相应地将它们分组。我希望能够将列“颜色”指定为设置微分器

采纳答案by MrE

Another simple way is to use the pivotfunction to format the data as you need first.

另一种简单的方法是使用该pivot函数首先根据需要格式化数据。

df.plot()does the rest

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()

result

结果

pivot effectively turns the data into:

数据透视有效地将数据转换为:

enter image description here

在此处输入图片说明

回答by unutbu

You could use groupbyto split the DataFrame into subgroups according to the color:

您可以使用groupby根据颜色将 DataFrame 拆分为子组:

for key, grp in df.groupby(['color']):


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_table('data', sep='\s+')
fig, ax = plt.subplots()

for key, grp in df.groupby(['color']):
    ax = grp.plot(ax=ax, kind='line', x='x', y='y', c=key, label=key)

plt.legend(loc='best')
plt.show()

yields enter image description here

产量 在此处输入图片说明

回答by saimadhu.polamuri

You can use this code to get your desire output

您可以使用此代码来获得您想要的输出

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

To plot graph

绘制图形

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()

OutputThe output result will look like this

输出输出结果将如下所示

回答by Cheng

If you have seaborninstalled, an easier method that does not require you to perform pivot:

如果您已经seaborn安装,一个更简单的方法不需要您执行pivot

import seaborn as sns

sns.lineplot(data=df, x='x', y='y', hue='color')