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
Plotting grouped data in same plot using Pandas
提问by user4522999
In Pandas, I am doing:
在熊猫中,我正在做:
bp = p_df.groupby('class').plot(kind='kde')
p_df
is a dataframe
object.
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 ax
keyword of DataFrameGroupBy.plot
to 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:
这是结果:
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:
这样您就可以轻松控制图例。这是结果:
回答by dagcilibili
Another approach would be using seaborn
module. 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.
这导致以下图像。
回答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())