我對 matplotlib.pyplot.bar 有問題,其中條形只能具有恒定的寬度,但我想繪制一個圖,其中條形位于兩點之間。正如我們在圖中看到的,點有不同的距離,我想用條形填充所有的間隙。抱歉,如果之前有人問過這個問題,我找不到它。i所以基本上我想把和之間的距離寬度作為點的條形i+1,如果這有意義的話。
1 回答

牧羊人nacy
TA貢獻1862條經驗 獲得超7個贊
以下示例創建一個具有 7 個高度值和 8 個邊界的條形圖。連續邊界之間的差值用作條寬度。條形默認與其中心對齊,但在處理寬度不均勻時,align='edge'則需要對齊。
from matplotlib import pyplot as plt
import numpy as np
bounds = np.array([21000, 31000, 41000, 53000, 62000, 73000, 81000, 90000])
heights = np.random.randint(50000, 120000, len(bounds) - 1)
plt.bar(bounds[:-1], heights, width=np.diff(bounds), align='edge', color='crimson', ec='navy')
# plt.xticks(bounds) # to have the boundaries as x ticks
plt.show()
添加回答
舉報
0/150
提交
取消