Matplotlib:箱线图和条形图在使用 twinx 叠加时移动

Matplotlib: Boxplot and bar chart shifted when overlaid using twinx

当我创建条形图并使用 twin x 覆盖条形图时,与条形相比,框看起来向右移动了一位。

此问题之前已发现 (),但解决方案似乎不再有效。 (我使用的是 Matplotlib 3.1.0)

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'])

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

输出显示与条形图相比,框向右移动了一位。前一个 post 的受访者正确地指出,柱形的刻度位置是从零开始的,而框的刻度位置是从一开始的,这导致了偏移。但是,受访者用来修复它的 plt.bar() 方法现在会抛出错误,因为 x 参数已成为强制性参数。如果提供了 x 参数,它仍然会抛出错误,因为不再有参数 'left'。

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)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-186-e257461650c1> in <module>
     26 plt.twinx()
     27 plt.bar(left=plt.xticks()[0], height=df.groupby('A').count()['B'],
---> 28         align='center', alpha=0.3)

TypeError: bar() missing 1 required positional argument: 'x'

此外,我更喜欢使用面向对象的方法参考坐标轴进行修复,因为我想将图表放入交互式 ipywidget 中。

这是理想的图表:

非常感谢。

您可以使用以下技巧:提供用于放置从 x=1 开始的条形的 x 值。为此,请使用 range(1, len(df_gb['B'])+1) 作为 x 值。

fig, ax = plt.subplots(figsize=(8, 4))
ax2 = ax.twinx()
df_gb = df.groupby('A').count()
df.boxplot(column='B', by='A', ax=ax)
ax2.bar(range(1, len(df_gb['B'])+1), height=df_gb['B'],align='center', alpha=0.3)