我应该如何通过函数传递 matplotlib 对象;作为轴,轴或图形?

通过函数传递 matplotlib 对象;作为轴,轴或图形,我们可以采取以下步骤 -

  • 设置图形大小并调整子图之间和周围的填充。

  • plot()方法中,在轴 ax 处绘制 x 和 y 数据点。

  • profile()方法中,创建一个图形和一组子图。迭代轴并传入plot()方法以绘制图形。

  • 调用profile()3 行 4 列的方法。

  • 要显示图形,请使用show()方法。

示例

import numpy as np
from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

def plot(ax, x, y):
   ax.plot(x, y)

def profile(rows, cols):
   fig, axes = plt.subplots(rows, cols)
   for ax in axes:
      for a in ax:
plot(a, np.random.rand(10), np.random.rand(10))

profile(3, 4)
plt.show()
输出结果

它将产生以下输出