Matplotlib:如何绘制两个直方图的差异?

2024-04-26 18:36:58 发布

您现在位置:Python中文网/ 问答频道 /正文

假设您有以下数据集:

a=[1, 2, 8, 9, 5, 6, 8, 5, 8, 7, 9, 3, 4, 8, 9, 5, 6, 8, 5, 8, 7, 9, 10]
b=[1, 8, 4, 1, 2, 4, 2, 3, 1, 4, 2, 5, 9, 8, 6, 4, 7, 6, 1, 2, 2, 3, 10]

假设你制作了他们的柱状图:

^{pr2}$

enter image description here

你如何用相同的箱子和两个柱状图之间的差异来制作一个柱状图?在

如果你这样做:

 diff=plt.bar([1,2,3,4,5,6,7,8,9,10], 
             height=(plot1[0]-plot2[0]), edgecolor='black', 
             linewidth=1.2, color='red',width = 1) 
 plt.title("a-b")

x轴上的值与存储箱不对齐。怎么解决这个问题? enter image description here


Tags: 数据bardiffplt差异colorblackheight
1条回答
网友
1楼 · 发布于 2024-04-26 18:36:58

好吧,如果我正确地理解了你的问题,那么解决方案就很简单了——如果你像前面两个直方图一样,把difference设置为从0开始,并将align设置为edge,那么它似乎工作得很好。在

diff=plt.bar([0,1,2,3,4,5,6,7,8,9], 
             height=(plot1[0]-plot2[0]), edgecolor='black', 
             linewidth=1.2, color='red',width = 1, align = 'edge') 
plt.title("a-b")
plt.show()

See my output plot

相关问题 更多 >