简体   繁体   English

Python pandas绘制移位x轴,如果是twx两个y轴

[英]Python pandas plotting shift x-axis if twinx two y-axes

I have a dataframe with 3 columns: one of them is a "groupby" column, the other two are "normal" columns with values. 我有一个包含3列的数据框:其中一列是“groupby”列,另外两列是带有值的“普通”列。 I want to generate a boxplot and a bar chart as well. 我想生成一个箱线图和一个条形图。 On the bar chart I want to visualize the number of occurences of each group's element. 在条形图上,我想要可视化每个组元素的出现次数。 Let my sample code tell this dataframe in more detailed: 让我的示例代码更详细地告诉这个数据帧:

li_str = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']

df = pd.DataFrame([[i]+j[k] for i,j in {li_str[i]:np.random.randn(j, 2).tolist() for i,j in \
    enumerate(np.random.randint(5, 15, len(li_str)))}.items() for k in range(len(j))]
    , columns=['A', 'B', 'C'])

So above I generate random number of random values to every element in li_str and I do it for columns B and C . 所以上面我为li_str每个元素生成随机数随机值,我为列BC

Then I visualize only a boxplot: 然后我只想象一个箱线图:

fig, ax = plt.subplots(figsize=(16,6))
p1 = df.boxplot(ax=ax, column='B', by='A', sym='')

My result is: 我的结果是: 在此输入图像描述

Now I visualize the number of elements every group has (so the random numbers I generated above with np.random.randint(5, 15, len(li_str)) code): 现在我可视化每个组具有的元素数量(因此我使用np.random.randint(5, 15, len(li_str))代码生成上面的随机数):

fig, ax = plt.subplots(figsize=(16,6))

df_gb = df.groupby('A').count()

p2 = df_gb['B'].plot(ax=ax, kind='bar', figsize=(16,6), colormap='Set2', alpha=0.3)
plt.ylim([0, 20])

My result is: 我的结果是: 在此输入图像描述

And now I want these two in one diagram: 现在我想在一个图中这两个:

fig, ax = plt.subplots(figsize=(16,6))
ax2 = ax.twinx()

df_gb = df.groupby('A').count()

p1 = df.boxplot(ax=ax, column='B', by='A', sym='')
p2 = df_gb['B'].plot(ax=ax2, kind='bar', figsize=(16,6)
    , colormap='Set2', alpha=0.3, secondary_y=True)
plt.ylim([0, 20])

My result is: 我的结果是: 在此输入图像描述

Does anybody know why my boxplot is shifted to right with one x-axis tick? 有人知道为什么我的箱图会向右移动一个x轴刻度吗? I use Python 3.5.1, pandas 0.17.0, matplotlib 1.4.3 我使用Python 3.5.1,pandas 0.17.0,matplotlib 1.4.3

Thank you!!! 谢谢!!!

It's because the boxplot and the bar plot do not use the same xticks even if the labels are the same. 这是因为即使标签相同,箱线图和条形图也不会使用相同的xticks。

df.boxplot(column='B', by='A')
plt.xticks()

(array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10]), <a list of 10 Text xticklabel objects>)

df.groupby('A').count()['B'].plot(kind='bar')
plt.xticks()

(array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), <a list of 10 Text xticklabel objects>)

At a glance it looks to me like an inconsistency which should be fixed in matplotlib boxplot() , but I might just be overlooking the rationale. 乍一看,它看起来像是一个不一致,应该在matplotlib boxplot()修复,但我可能只是忽略了理由。

As a workaround use matplotlib bar() , that allows you to specify the xticks to match those of the boxplot (I did not found a way to do it with df.plot(kind='bar') . 作为一种解决方法,使用matplotlib bar() ,它允许你指定xticks以匹配boxplot的那些(我没有找到一种方法来使用df.plot(kind='bar')

df.boxplot(column='B', by='A')
plt.twinx()
plt.bar(left=plt.xticks()[0], height=df.groupby('A').count()['B'],
        align='center', alpha=0.3)

在此输入图像描述

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

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