如何用条形图绘制条形图?

时间:2018-08-23 07:50:13

标签: python matplotlib

我正在尝试使用python绘制条形字符。以下是我的代码。我的每个酒吧都获得了0的价值。

from __future__ import division
import matplotlib.pyplot as plt
import numpy as np

x = [0,1,2,3,4,5,6,7,8,9,10,11,12]
freq = [0.93, 0.87,0.86,0.87,0.93,0.84,0.74,0.79,0.78,0.95,0.88,0.8, 0.71]

width = 0.5 # width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x, freq, width, color='r')

ax.set_ylim(0,1)
ax.set_ylabel('auc-roc')

ax.set_xticks(np.add(x,(width/2.2))) # set the position of the x ticks
ax.set_xticklabels(('X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8', 'X9', 'X10', 'X11', 'X12', 'X13'))

def autolabel(rects):

    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2., 1.0*height,
            '%d' % (height),
            ha='center', va='bottom')

autolabel(rects1)

plt.show()

Image with 0 bar values

1 个答案:

答案 0 :(得分:2)

'%d' % (height),

我认为这会将值映射为整数。在您的情况下,值以0开头。

使用%f或更确切地说%.2f

enter image description here