且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在python中为子图运行智能循环

更新时间:2022-06-08 22:40:38

一种迭代许多子图的简单方法是通过

An easy way to iterate over many subplots is to create them via

fig, axes = plt.subplots(nrows=n, ncols=m)

那么

axes 是一个 n 行和 m 列的数组.然后,可以独立地遍历行和列,或遍历轴数组的展平版本.

axes is then an array of n rows and m columns. One could then iterate over the rows and columns independently, or over the flattened version of the axes array.

在此示例中显示了后者:

The latter is shown in this example:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(11)
y = np.random.rand(len(x), 9)*10

fig, axes = plt.subplots(3,3, sharex=True, sharey=True)

for i, ax in enumerate(axes.flatten()):
    ax.bar(x, y[:,i], color=plt.cm.Paired(i/10.))

plt.show()