简体   繁体   English

使用 Pandas 数据框以不同颜色绘制多条线

[英]Plotting multiple lines, in different colors, with pandas dataframe

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我希望能够将列color指定为设置的微分器

Another simple way is to use the pandas.DataFrame.pivot function to format the data.另一种简单的方法是使用pandas.DataFrame.pivot函数来格式化数据。

Use pandas.DataFrame.plot to plot.使用pandas.DataFrame.plot进行绘图。 Providing the colors in the 'color' column exist in matplotlib: List of named colors , they can be passed to the color parameter.如果matplotlib: List of named colors 中存在'color'列中 的颜色,则可以将它们传递给color参数。

# sample data
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'])

# pivot the data into the correct shape
df = df.pivot(index='x', columns='color', values='y')

# display(df)
color  blue  red
x               
0         0    0
1         1    1
2         4    2
3         9    3
4        16    4
5        25    5
6        36    6
7        49    7
8        64    8
9        81    9

# plot the pivoted dataframe; if the column names aren't colors, remove color=df.columns
df.plot(color=df.columns, figsize=(5, 3))

在此处输入图片说明

You could use groupby to 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产量在此处输入图片说明

If you have seaborn installed, 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')

You can also try the following code to plot multiple lines in different colors with pandas data frame.您还可以尝试使用以下代码使用 Pandas 数据框以不同颜色绘制多条线。

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from pandas import DataFrame

value1 = [10, 20, 30, 40, 50] 
value2 = [5, 10, 15, 20, 25]
value3 = [8, 9, 10, 15, 20]

results1 = DataFrame({'SAC': value1, 'TD3': value2, 'DDPG': value3})

results1.plot()
plt.legend(loc='lower right')
plt.xlabel("Episode")
plt.ylabel("Rewards")
plt.show()

Output:输出:

在此处输入图片说明

The most general way is to plot the different color based on the color group.最通用的方法是根据color组绘制不同的color That is, we use Dataframe.groupby to group the colors and then plot the data on the relevant axes.也就是说,我们使用Dataframe.groupby对颜色进行分组,然后在相关轴上绘制数据。

For example例如

import numpy as np, pandas as pd, matplotlib.pyplot as plt
n = 1000
xy = np.random.rand(n,  2) + np.random.rand(n)[:, None]
color = np.random.randint(0, 3, size = n)
data = dict(x = xy[:, 0], y = xy[:, 1], color = color)
df = pd.DataFrame(data)

fig, ax = plt.subplots()
for labels, dfi in df.groupby("color"):
    dfi.plot(ax = ax, x = 'x', y = 'y', label = labels)
ax.legend(title = 'color')
fig.show()

在此处输入图片说明

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

Output输出输出结果将如下所示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM