按照如下方法导入需要的库:

import numpy as np
import matplotlib.pyplot as plt

条形图

# prepare data
people = ('John', 'Allair', 'Hadley', 'Yihui', 'Patrick')
y_pos = np.arange(len(people))
performance = np.random.rand(len(people)) * 10 + 3

plt.barh(y_pos, performance, alpha=0.4)
plt.yticks(y_pos, people)
plt.xlabel('Performance')
plt.title('How fast do you want to go today?')

plt.show()

柱形图

mu = 100
sigma = 15
x = mu + sigma * np.random.randn(10000)

num_bins = 50
plt.hist(x, num_bins, normed=True, facecolor="green", alpha=0.5)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')
plt.show()

多个柱形图

n_bins = 10
x = np.random.randn(1000, 3)

fig, axes = plt.subplots(nrows=2, ncols=2)
ax0, ax1, ax2, ax3 = axes.flat

colors = ['red', 'tan', 'lime']
ax0.hist(x, n_bins, normed=1, histtype='bar', color=colors, label=colors)
ax0.legend(prop={'size': 10})
ax0.set_title('bars with legend')

ax1.hist(x, n_bins, normed=1, histtype='bar', stacked=True)
ax1.set_title('stacked bar')

ax2.hist(x, n_bins, normed=1, histtype='step', stacked=True, fill=True)
ax2.set_title('stepfilled')

x_multi = [np.random.rand(n) for n in [10000, 5000, 2000]]
ax3.hist(x_multi, n_bins, histtype='bar')
ax3.set_title('different sample sizes')

plt.tight_layout()
plt.show()