fantastic matplotlib第四回

  1. 理论树:本节主要是关于在图上写字。自然包括了图中的标签、坐标轴标签。

  2. 思考题:做出下图。


    答:关键是查一下bar_label。

import matplotlib.pyplot as plt

n = [1, 2, 3, 4, 5]
names = ['Jim', 'Slim', 'Harry', 'Dick', 'Tom']
performance = [12.8, 8.5, 10.2, 10.4, 10]
error = [0.71, 0.25, 0.16, 0.70, 0.72]


fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot()
ax.set_title('How fast do you want to go today?', fontsize=18)
br = ax.barh(n, performance, xerr = error, align = 'center')
ax.bar_label(br, labels = ['±%.2f' % e for e in error],
             padding = 8, color = 'blue', fontsize = 20)
ax.set_xlabel('Performance', fontsize=20)
ax.set_xticks(np.arange(0, 17, 2), fontsize=20)
ax.set_yticks(n, labels = names, fontsize=20);

浙ICP备19012682号