matplotlib的基本用法(八)——绘制柱状图

来源:互联网 发布:ubuntu恢复初始命令 编辑:程序博客网 时间:2024/05/19 10:40

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

本文主要使用matplotlib进行柱状图的绘制。

  • Demo
import matplotlib.pyplot as pltimport numpy as np# 数据数目n = 10x = np.arange(n)# 生成数据, 均匀分布(0.5, 1.0)之间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 = 'blue', edgecolor = 'white')# 绘制柱状图, 向下plt.bar(x, -y2, facecolor = 'green', edgecolor = 'white')temp = zip(x, y2)# 在柱状图上显示具体数值, ha水平对齐, va垂直对齐for x, y in zip(x, y1):    plt.text(x + 0.05, y + 0.1, '%.2f' % y, ha = 'center', va = 'bottom')for x, y in temp:    plt.text(x + 0.05, -y - 0.1, '%.2f' % y, ha = 'center', va = 'bottom')# 设置坐标轴范围plt.xlim(-1, n)plt.ylim(-1.5, 1.5)# 去除坐标轴plt.xticks(())plt.yticks(())plt.show()
  • 结果

柱状图

0 0
原创粉丝点击