如何绘制和注释分组的条
我有以下代码
import numpy as np
import matplotlib.pyplot as plt
oct_data = [10, 24, 25, 30]
nov_data = [12, 42, 21, 78]
labels = ['Account_1', 'Account_2', 'Account_3', 'Account_4']
bar_width = 0.4
rect_1 = np.arange(0, len(oct_data)*2 ,2)
rect_2 = [x + bar_width for x in rect_1]
plt.bar(rect_1, oct_data, color='#7f6d5f', width=bar_width, edgecolor='white', label='Month_1')
plt.bar(rect_2, nov_data, color='#557f2d', width=bar_width, edgecolor='white', label='Month_2')
plt.ylabel('Cost ($)', fontsize=10)
plt.legend()
plt.show()
如您所见Account_2,... )不集中。 据我了解,此命令应该完成这项工作,但事实并非如此。
plt.xticks([r + bar_width for r in range(0, len(oct_data)*2, 2)], labels)
我也想在酒吧内添加heigh的价值。通常,这就是我使用“单栏”图进行操作的方式:
for i in range(len(labels)):
plt.text(i, oct_data[i]//2, oct_data[i], ha = 'center', color = 'black')
但这在这里不起作用。
任何帮助将不胜感激。我是Matplotlib的总体初学者。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
matplotlib
作为默认绘图后端。将列绘制为条形组,索引是独立的轴。matplotlib 3.4.2
, 应将其用于栏上的注释。.bar_label
的添加信息和示例和如何绘制和注释分组的条形图以获取分组栏的其他示例。python 3.9.7
,pandas 1.3.4
,matplotlib 3.4.3
pandas.DataFrame.plot
, which usesmatplotlib
as the default plotting backend. Columns are plotted as the bar groups and the index is the independent axis.matplotlib 3.4.2
,.bar_label
should be used for annotations on bars..bar_label
, and How to plot and annotate a grouped bar chart for an additional example of grouped bars.python 3.9.7
,pandas 1.3.4
,matplotlib 3.4.3
有
Align
您可以使用:为了注释,建议具有轴实例:
output:
更新:所有代码:
There is
align
option you can use:For annotation, it's recommend to have an axis instance:
Output:
Update: All code: