Python 3.8: matplotlib.pyplot.contour 中的 z 数组大小错误

Python 3.8: z array size error in matplotlib.pyplot.contour

我正在尝试使用 numpy.meshgrid 和 matplotlib.pyplot.contour 绘制多条线。

像这样:

import numpy as np
import matplotlib.pyplot as plt

l1 = [[1.14524340102759, 0.0, 0.876379777564501, 0.0774032782000112],
 [0.698164152459634, 0.0, 0.577238912623023, 0.0334322477435396],
 [1.56063241621204, 0.0, 1.12917068564018, 0.129171162657132],
 [0.925893720680833, 0.0, 0.733948881787323, 0.0541614422907881],
 [1.35667899082301, 0.0, 1.00744147250486, 0.102545387706258]]

l2 = [(-3.33066845080019, -11.5692204395060),
 (-3.20810804556752, -11.6038035434509),
 (-3.45285619481352, -11.5333428807513),
 (-3.26943397473207, -11.5866740435686),
 (-3.39180976091197, -11.5514432194779)]

for i, j in zip(l1, l2):
    
    x4, y4 = np.meshgrid(np.arange(i[2], i[0], 0.05),np.arange(i[1], i[3], 0.05))
    
    plt.contour(x4, y4, j[0] * (x4 - i[0]) + j[1] * (y4 - i[1]) , [0], linewidths = 0.5, colors = 'crimson')

但是我收到这个错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-f93f79730069> in <module>
      9     x4, y4 = np.meshgrid(np.arange(i[2], i[0], 0.05),np.arange(i[1], i[3], 0.05))
     10 
---> 11     plt.contour(x4, y4, j[0] * (x4 - i[0]) + j[1] * (y4 - i[1]) , [0], linewidths = 0.5, colors = 'crimson')

~\anaconda3\lib\site-packages\matplotlib\pyplot.py in contour(data, *args, **kwargs)
   2565 @_copy_docstring_and_deprecators(Axes.contour)
   2566 def contour(*args, data=None, **kwargs):
-> 2567     __ret = gca().contour(
   2568         *args, **({"data": data} if data is not None else {}),
   2569         **kwargs)

~\anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
   1436     def inner(ax, *args, data=None, **kwargs):
   1437         if data is None:
-> 1438             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1439 
   1440         bound = new_sig.bind(ax, *args, **kwargs)

~\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in contour(self, *args, **kwargs)
   6330     def contour(self, *args, **kwargs):
   6331         kwargs['filled'] = False
-> 6332         contours = mcontour.QuadContourSet(self, *args, **kwargs)
   6333         self._request_autoscale_view()
   6334         return contours

~\anaconda3\lib\site-packages\matplotlib\contour.py in __init__(self, ax, levels, filled, linewidths, linestyles, hatches, alpha, origin, extent, cmap, colors, norm, vmin, vmax, extend, antialiased, nchunk, locator, transform, *args, **kwargs)
    814         self._transform = transform
    815 
--> 816         kwargs = self._process_args(*args, **kwargs)
    817         self._process_levels()
    818 

~\anaconda3\lib\site-packages\matplotlib\contour.py in _process_args(self, corner_mask, *args, **kwargs)
   1428             self._corner_mask = corner_mask
   1429 
-> 1430             x, y, z = self._contour_args(args, kwargs)
   1431 
   1432             _mask = ma.getmask(z)

~\anaconda3\lib\site-packages\matplotlib\contour.py in _contour_args(self, args, kwargs)
   1486             args = args[1:]
   1487         elif Nargs <= 4:
-> 1488             x, y, z = self._check_xyz(args[:3], kwargs)
   1489             args = args[3:]
   1490         else:

~\anaconda3\lib\site-packages\matplotlib\contour.py in _check_xyz(self, args, kwargs)
   1519             raise TypeError(f"Input z must be 2D, not {z.ndim}D")
   1520         if z.shape[0] < 2 or z.shape[1] < 2:
-> 1521             raise TypeError(f"Input z must be at least a (2, 2) shaped array, "
   1522                             f"but has shape {z.shape}")
   1523         Ny, Nx = z.shape

TypeError: Input z must be at least a (2, 2) shaped array, but has shape (1, 3)

我在绘制的另一组列表中遇到了类似的问题,但问题是我的 xmin 和 xmax 值在网格中的位置错误。但这似乎不是这里的问题。所以我不明白我到底做错了什么。

请帮忙。

你的问题是列表的第二个元素l1

[0.698164152459634, 0.0, 0.577238912623023, 0.0334322477435396],

此数组的最后一个元素 0.0334 小于您在此处 np.arange 调用中使用的步长

np.arange(i[1], i[3], 0.05))

如果我们填写值

>>> np.arange(0.0, 0.0334322477435396, 0.05)
array([0.])

l1 中的任何其他条目都不会发生这种情况。解决这个问题(例如,将步长设置为小于该值),它将解决您的问题。