matplotlib-柱状图 发表于 2019-06-10 | 分类于 python 详情请看莫烦老师教程莫烦python 代码123456789101112131415161718192021222324252627import matplotlib.pyplot as pltimport numpy as npn = 12X = np.arange(n)Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')for x, y in zip(X, Y1): # ha: horizontal alignment # va: vertical alignment plt.text(x, y + 0.05, '%.2f' % y, ha='center', va='bottom')for x, y in zip(X, Y2): # ha: horizontal alignment # va: vertical alignment plt.text(x, -y - 0.05, '-%.2f' % y, ha='center', va='top')plt.xlim(-.5, n)plt.xticks(())plt.ylim(-1.25, 1.25)plt.yticks(())plt.show() 结果