Python 将数据帧绘制为';历史';和';kde';同谋
我有一个包含用户信息的数据框。我想在同一个图上绘制Python 将数据帧绘制为';历史';和';kde';同谋,python,pandas,matplotlib,plot,dataframe,Python,Pandas,Matplotlib,Plot,Dataframe,我有一个包含用户信息的数据框。我想在同一个图上绘制kind='kde'和kind='hist'上的用户年龄。目前,我能够拥有两个独立的情节。数据帧类似于: member_df= user_id Age 1 23 2 34 3 63 4 18 5 53 ... 使用 ax1 = plt.subplot2grid((2,3), (0,0)) member_df.Age.plot(kind=
kind='kde'
和kind='hist'
上的用户年龄。目前,我能够拥有两个独立的情节。数据帧类似于:
member_df=
user_id Age
1 23
2 34
3 63
4 18
5 53
...
使用
ax1 = plt.subplot2grid((2,3), (0,0))
member_df.Age.plot(kind='kde', xlim=[16, 100])
ax1.set_xlabel('Age')
ax2 = plt.subplot2grid((2,3), (0,1))
member_df.Age.plot(kind='hist', bins=40)
ax2.set_xlabel('Age')
ax3 = ...
我知道kind='kde'
会给我y轴的频率,而kind='kde'
会给我一个累积分布,但是有没有办法将两者结合起来,让y轴由频率来表示?pd.DataFrame.plot()
返回它要打印的ax
。您可以将其重新用于其他绘图
尝试:
示例我首先绘制
hist
以放入背景另外,我将
kde
放在secondary_y
轴上
import pandas as pd
import numpy as np
np.random.seed([3,1415])
df = pd.DataFrame(np.random.randn(100, 2), columns=list('ab'))
ax = df.a.plot(kind='hist')
df.a.plot(kind='kde', ax=ax, secondary_y=True)
评论回复
使用
subplot2grid
。只需重新使用ax1即可
import pandas as pd
import numpy as np
ax1 = plt.subplot2grid((2,3), (0,0))
np.random.seed([3,1415])
df = pd.DataFrame(np.random.randn(100, 2), columns=list('ab'))
df.a.plot(kind='hist', ax=ax1)
df.a.plot(kind='kde', ax=ax1, secondary_y=True)
如果您希望数据帧的所有列都使用它:
fig, ax = plt.subplots(8,3, figsize=(20, 50))
# you can change the distribution, I had 22 columns, so 8x3 is fine to me
fig.subplots_adjust(hspace = .2, wspace=.2, )
ax = ax.ravel()
for i in range(len(I_df.columns)):
ax[i] = I_df.iloc[:,i].plot(kind='hist', ax=ax[i])
ax[i] = I_df.iloc[:,i].plot(kind='kde', ax=ax[i], secondary_y=True)
plt.title(I_df.columns[i])
我希望它能有所帮助:)它使用起来更好,甚至更简单。之前提出的解决方案让KDE图对我来说有点“上移”<代码>seaborn.distplot在hist和kde绘图之间精确地排列了零
导入seaborn作为sns
sns显示(df.a)
我已经测试了代码,并尝试根据需要对其进行轻微修改。当我只有这两个情节需要考虑时,这就是诀窍。当我试图在sublot2grid
中包含时,它无法产生相同的结果,它只会复制直方图。@Lukasz在任何情况下都要使用相同的ax
。
fig, ax = plt.subplots(8,3, figsize=(20, 50))
# you can change the distribution, I had 22 columns, so 8x3 is fine to me
fig.subplots_adjust(hspace = .2, wspace=.2, )
ax = ax.ravel()
for i in range(len(I_df.columns)):
ax[i] = I_df.iloc[:,i].plot(kind='hist', ax=ax[i])
ax[i] = I_df.iloc[:,i].plot(kind='kde', ax=ax[i], secondary_y=True)
plt.title(I_df.columns[i])