简体   繁体   中英

Double bar plot matplotlib

I would like to have a double bar plot with matplotlib . This is my code:

width = 0.55   
ax2.set_xlim(0,len(partiteIndici)+width)
plt.ylim([0,100])
rects1 = ax2.bar(partiteIndici, distanze, width,align='center', color='blue', label='4-3-3 Modello')
rects2 = ax2.bar(partiteIndici, distanze2, width,align='center', color='red', label='Squadra X')
plt.xticks(index + width, partite)
plt.legend()
plt.tight_layout()
plt.show()

But this is what I have: 在此输入图像描述

I would like to have blue bars next to red bars, not overlapped. How can I get this working?

And why do I have the markers on the x bar shifted respect to bars?

I see two errors in your code: first, you get your bar plots overlapped because you plot them with the same values of x ; you need to manually shift one plot to get it working your way, as per this example . Assuming partiteIndici is an array, change rects2 line to

rects2 = ax2.bar(partiteIndici + width, distanze2, width,align='center', color='red', label='Squadra X')

This will get one of your plots shifted. As per ticks and labels, you have to put them to the same x points as bars; change index in xticks line to partiteIndici :

plt.xticks(partiteIndici + width, partite)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM