【问题标题】:Pandas plot barh with centered bars (pyramid)Pandas 用居中的条(金字塔)绘制 barh
【发布时间】:2019-10-31 05:35:44
【问题描述】:

我正在尝试使用 pandas.plot(kind='barh') 创建一个绘图,但在绘图中间(从 X 轴角度看是中间)获得居中的框。

我试图得到这样的东西: https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-python/#29.-Population-Pyramid

使用此代码:

ax = df.May2019.T.plot(kind='barh', subplots = False, stacked=True,
          figsize=(12,8), title = 'Number of customers per revenue slice',
          width=0.99)
vals = ax.get_yticks()
ax.set_yticklabels(['60%-80%', '40%-60%', '20%-40%', '0%-20%'])
ax.set_ylabel('Top x% Revenue')
ax.set_xlabel('Number of customers in revenue band')

我得到了类似下图的东西,这是可以预料的:

我希望将所有框居中放置在图表中间。我知道 X 轴会变得不那么有意义,但我正在寻找图像的价值。

我的 pd.Series 看起来像这样

0.8    75
0.6    26
0.4    10
0.2     3
Name: May2019, dtype: int64

关于我应该查看/更改什么的任何建议?

【问题讨论】:

  • 您可以将ax.set_xlim(-ax.get_xlim()[1], None) 设置为轴中间的0。你是这个意思吗?如果不是,请多花点时间描述问题。
  • 另外,请提供一个代码,可以简单地复制粘贴并执行以获得发布的数字。以可复制的格式发布数据框

标签: python pandas matplotlib charts


【解决方案1】:

绘制正负两半:

fig, ax = plt.subplots(figsize=(6, 4))
(df.May2019.T/2).plot(kind='barh',
                      title='Number of customers per revenue slice',
                      width=0.99, ax=ax)
(df.May2019.T/-2).plot(kind='barh', width=0.99, ax=ax)

ax.set_yticklabels(['60%-80%', '40%-60%', '20%-40%', '0%-20%'])
ax.set_ylabel('Top x% Revenue')
ax.set_xlabel('Number of customers in revenue band')
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    相关资源
    最近更新 更多