gpt4 book ai didi

python - Matplotlib - 查找条形图宽度值

转载 作者:行者123 更新时间:2023-11-28 21:42:13 26 4
gpt4 key购买 nike

希望这是一个相对简单的问题 - 我正在绘制两个条形图。其中一个有更多的类别,因此每个条形的宽度更小。请参阅下面的示例代码/图表(简单的标准绘图):

fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)

ax1.set_title("Sample Graph", size='16')
ax1.bar(df1.index, df1.c_destination, color='purple', alpha =1)
ax1.set_axisbelow(True)
ax1.set_xticks(df1.index)
ax1.set_xticklabels(df1['index'], rotation=45, ha='right', size='14')
ax1.patch.set_facecolor(color='gray')
ax1.patch.set_alpha(0.5)

ax2.set_title("Sample Graph Other", size='16')
ax2.bar(df2.index, df2.destination, color='purple', alpha =1)
ax2.set_axisbelow(True)
ax2.set_xticks(df2.index)
ax2.set_xticklabels(df2['index'], rotation=45, ha='right', size='14')
ax2.patch.set_facecolor(color='gray')
ax2.patch.set_alpha(0.5)

如果左图有 12 个条,右图有 8 个条,如何确保条数较少的图具有相同的条宽?我知道我可以在每个中定义宽度,但我想知道 ax2 是否可以从 ax1 访问这个值?感谢您的帮助。

最佳答案

In [147]: [p.get_width() for p in ax1.patches]
Out[147]: [0.5, 0.5, 0.5, 0.5, 0.5]

关于python - Matplotlib - 查找条形图宽度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43987448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com