Python Matplotlib将文本置于直方图b之外

2024-10-01 04:47:12 发布

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

我有以下生成直方图的Python代码

data_array=[1,1,4,2,10,1]
num_bins = 12
fig = plt.figure(1, figsize=[16,9])
fig.suptitle("title")
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
sub = plt.subplot(111)
hist_array = sub.hist(data_array, bins=num_bins, color='#54c29a', orientation='horizontal', edgecolor='black', linewidth=1)
for i in range(num_bins):
    if hist_array[0][i] > 0:
        sub.text(hist_array[0][i], hist_array[1][i], str(int(hist_array[0][i])), horizontalalignment="right", verticalalignment="bottom")

上述代码生成以下直方图: enter image description here

我想做的是显示直方图条外的文本,如下图所示: enter image description here

我该怎么做?我试过了horizontalalignment='right',但没用


Tags: 代码rightdatafigplt直方图arraynum