Python 使用 Pandas 在同一图中绘制分组数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28293028/
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 03:04:19  来源:igfitidea点击:

Plotting grouped data in same plot using Pandas

pythonpandasplot

提问by user4522999

In Pandas, I am doing:

在熊猫中,我正在做:

bp = p_df.groupby('class').plot(kind='kde')

p_dfis a dataframeobject.

p_df是一个dataframe对象。

However, this is producing two plots, one for each class. How do I force one plot with both classes in the same plot?

但是,这会产生两个图,每个类一个。如何在同一情节中强制两个班级的情节?

回答by cel

Version 1:

版本 1:

You can create your axis, and then use the axkeyword of DataFrameGroupBy.plotto add everything to these axes:

您可以创建轴,然后使用ax关键字 ofDataFrameGroupBy.plot将所有内容添加到这些轴:

import matplotlib.pyplot as plt

p_df = pd.DataFrame({"class": [1,1,2,2,1], "a": [2,3,2,3,2]})
fig, ax = plt.subplots(figsize=(8,6))
bp = p_df.groupby('class').plot(kind='kde', ax=ax)

This is the result:

这是结果:

plot

阴谋

Unfortunately, the labeling of the legend does not make too much sense here.

不幸的是,这里的图例标签没有太大意义。

Version 2:

版本 2:

Another way would be to loop through the groups and plot the curves manually:

另一种方法是遍历组并手动绘制曲线:

classes = ["class 1"] * 5 + ["class 2"] * 5
vals = [1,3,5,1,3] + [2,6,7,5,2]
p_df = pd.DataFrame({"class": classes, "vals": vals})

fig, ax = plt.subplots(figsize=(8,6))
for label, df in p_df.groupby('class'):
    df.vals.plot(kind="kde", ax=ax, label=label)
plt.legend()

This way you can easily control the legend. This is the result:

这样您就可以轻松控制图例。这是结果:

plot2

情节2

回答by dagcilibili

Another approach would be using seabornmodule. This would plot the two density estimates on the same axes without specifying a variable to hold the axes as follows (using some data frame setup from the other answer):

另一种方法是使用seaborn模块。这将在同一轴上绘制两个密度估计值,而不指定一个变量来保持轴如下(使用其他答案中的一些数据框设置):

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

# data to create an example data frame
classes = ["c1"] * 5 + ["c2"] * 5
vals = [1,3,5,1,3] + [2,6,7,5,2]
# the data frame 
df = pd.DataFrame({"cls": classes, "indices":idx, "vals": vals})

# this is to plot the kde
sns.kdeplot(df.vals[df.cls == "c1"],label='c1');
sns.kdeplot(df.vals[df.cls == "c2"],label='c2');

# beautifying the labels
plt.xlabel('value')
plt.ylabel('density')
plt.show()

This results in the following image.

这导致以下图像。

Resulting image from the code given above.

上面给出的代码生成的图像。

回答by Udit Choudhary

Maybe you can try this:

也许你可以试试这个:

fig, ax = plt.subplots(figsize=(10,8))
classes = list(df.class.unique())
for c in classes:
    df2 = data.loc[data['class'] == c]
    df2.vals.plot(kind="kde", ax=ax, label=c)
plt.legend()

回答by sp00n3r

import matplotlib.pyplot as plt
p_df.groupby('class').plot(kind='kde', ax=plt.gca())