有两种方法:
- 使用子图对象的轴方法(例如ax.set_xticks和ax.set_xticklabels)
- 使用plt.sca设置pyplot状态机的当前轴(即plt接口)。
作为示例(这也说明了使用setp来更改所有子图的属性):
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=3, ncols=4)
# Set the ticks and ticklabels for all axes
plt.setp(axes, xticks=[0.1, 0.5, 0.9], xticklabels=['a', 'b', 'c'],
yticks=[1, 2, 3])
# Use the pyplot interface to change just one subplot...
plt.sca(axes[1, 1])
plt.xticks(range(3), ['A', 'Big', 'Cat'], color='red')
fig.tight_layout()
plt.show()
其中一些api的参考文档:
matplotlib.pyplot.sca
matplotlib.pyplot.setp