文章作者:Tyan
博客:noahsnail.com | CSDN | 簡(jiǎn)書(shū)
本文主要使用matplotlib進(jìn)行柱狀圖的繪制缤言。
- Demo
import matplotlib.pyplot as plt
import numpy as np
# 數(shù)據(jù)數(shù)目
n = 10
x = np.arange(n)
# 生成數(shù)據(jù), 均勻分布(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)
# 在柱狀圖上顯示具體數(shù)值, ha水平對(duì)齊, va垂直對(duì)齊
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')
# 設(shè)置坐標(biāo)軸范圍
plt.xlim(-1, n)
plt.ylim(-1.5, 1.5)
# 去除坐標(biāo)軸
plt.xticks(())
plt.yticks(())
plt.show()
- 結(jié)果