此示例展示了一个简单的水平条形图。
示例代码
#! /usr/bin/env python
#coding=utf-8
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
plt.rcdefaults()
fig, ax = plt.subplots()
plt.rcParams['font.sans-serif'] = ['SimHei'] #
plt.rcParams['axes.unicode_minus'] = False
# 原文出自【易百教程】,商业转载请联系作者获得授权,非商业请保留原文链接。
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))
ax.barh(y_pos, performance, xerr=error, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis() # labels read top-to-bottom
ax.set_xlabel('性能')
ax.set_title('你今天想要多快?')
plt.show()
执行上面示例代码,得到以下结果 -