绘制多轴对象数组

时间:2019-08-30 06:48:31

标签: python matplotlib plot

我有一个4x4的numpy数组ax,其中每个元素都是一个<class 'matplotlib.axes._subplots.AxesSubplot'>对象。

ax的外观

>>> ax
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x124366f98>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1246d0160>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x124392710>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x125c0cdd8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x1270032b0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x127124908>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x12afcaf98>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x127176668>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x127170cf8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x124b1e3c8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1270c2a58>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x124aeb128>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x124b0d7b8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x12af52e48>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x124a96518>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x12910cba8>]],
      dtype=object)

我需要将这些图形中的每个图形绘制为四乘四图形中的子图形。这就是我想要得到的(忽略此图的尺寸)enter image description here

我尝试了简单的plt.show(ax),但是由于axAxesSubplot类的集合,因此我显然遇到了错误。我也尝试遍历ax数组并作图,但是遇到的错误与运行plt.show(ax)时发生的错误相同。这是错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 253, in show
    return _show(*args, **kw)
  File "/usr/local/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 207, in show
    if block:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

如何绘制AxesSubplot类的集合?

1 个答案:

答案 0 :(得分:0)

您必须遍历axesSubplot实例并调用fig.axes.append(ax)