用正确的标签绘制三个子图条形图

破坏性的

我似乎无法使绘图正确使用标签。这些图的工作方式是生成三个子图条形图。但是我想用label标记每个图(3)cr_lst如何确保我可以cr_lst在每个栏和上面标记每个栏。

plt.figure(0)
width = 0.35       # the width of the bars
cr_lst = ['A', 'B', 'C', 'D']
A_lst = [1, 2, 3, 4]
B_lst = [2, 2, 6, 7]
A_lst = [8, 8, 6, 7]
ind = np.arange(len(A_lst))  # the x locations for the groups
f, axarr = plt.subplots(3, sharex=True)
axarr[0].set_title('Three plots\n')
axarr[0].set_ylabel('A')
axarr[1].set_ylabel('B')
axarr[2].set_ylabel('C')
axarr[0].set_ylim(ymin=0.001,ymax=max(A_lst)*1.10)
axarr[1].set_ylim(ymin=0.001,ymax=max(B_lst)*1.10)
axarr[2].set_ylim(ymin=0.001,ymax=max(B_lst)*1.10)
axarr[0].grid()
axarr[1].grid()
axarr[2].grid()
rects1 = axarr[0].bar(ind, A_lst, width, color='r',  linewidth=1,alpha=0.8, label=cr_lst)
rects2 = axarr[1].bar(ind, B_lst, width, color='y', linewidth=1,alpha=0.8, label=cr_lst)
rects3 = axarr[2].bar(ind, C_lst, width, color='blue',  linewidth=1, alpha=0.8, label=cr_lst) 
plt.savefig("ByC.png")

我想在x轴上显示标签。

底漆

这将为您提供每个轴上每个条形下方的标签:

width = 0.35       # the width of the bars
cr_lst = ['A', 'B', 'C', 'D']
x = range(len(cr_lst)) # the x locations for the groups
A_lst = [1, 2, 3, 4]
B_lst = [2, 2, 6, 7]
C_lst = [8, 8, 6, 7]
f, axarr = plt.subplots(3, sharex=False)
axarr[0].set_title('Three plots\n')
axarr[0].set_ylabel('A')
axarr[1].set_ylabel('B')
axarr[2].set_ylabel('C')
axarr[0].set_ylim(ymin=0.001,ymax=max(A_lst)*1.10)
axarr[1].set_ylim(ymin=0.001,ymax=max(B_lst)*1.10)
axarr[2].set_ylim(ymin=0.001,ymax=max(B_lst)*1.10)
axarr[0].grid()
axarr[1].grid()
axarr[2].grid()
rects1 = axarr[0].bar(x, A_lst, width, color='r', align='center', linewidth=1,alpha=0.8)
rects2 = axarr[1].bar(x, B_lst, width, color='y', align='center', linewidth=1,alpha=0.8)
rects3 = axarr[2].bar(x, C_lst, width, color='blue', align='center', linewidth=1, alpha=0.8) 
for ax in axarr:
    ax.set_xticks(x)
    ax.set_xticklabels(cr_lst)

plt.savefig("ByC.png")

请注意,share=False在中plt.subplots如果将其设置为它,True则将隐藏除最低斧头以外的所有其他标签。

另请注意align='center'in中的使用.bar()

这将产生: 在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章