[英]How to remove gaps between bars with unequal widths in Matplotlib bar chart
這是一個快速的解決方案,我把它放在一起,雖然我覺得在matplotlib中可能存在gaps=0
選項,但我找不到文檔。
fig, ax = plt.subplots()
cost = [1, 1.5, 2, 3, 4]
width = [1.0, 5.0, 10.0, 15.0, 10.0]
y_pos = [1,2,3,4,5]
colors = ['red','blue','green','red','blue']
bars = ['A','B','C','D','E']
relative_positions = []
relative_position = 0
previous_width = 0
for w in width:
print(w, w/2)
relative_position = float(relative_position + (previous_width/2) + (w/2))
print(w, relative_position)
previous_width = w
relative_positions.append(relative_position)
plt.bar(relative_positions, cost, width=width, color=colors)
plt.xticks(relative_positions, bars)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.