简体   繁体   English

pyplot.contourf()在指定的levels参数时返回错误

[英]pyplot.contourf() returns error when specified levels argument

EDIT: The issue is most likely about the version. 编辑:问题很可能与版本有关。 The levels argument takes an integer argument in version 3.0.0, while this issue has occurred while using version 2.2.2. levels参数在版本3.0.0中采用整数参数,而在使用版本2.2.2时发生此问题。

UPDATE: The issue didn't occur after installing the version >=3.0.0. 更新:安装版本> = 3.0.0后没有出现此问题。

I'm trying to make a contour plot in Python using the matplotlib.pyplot.contourf() function, and it works perfectly like this: 我正在尝试使用matplotlib.pyplot.contourf()函数在Python中创建等高线图,它的工作原理如下:

plt.contourf(x, y, z)

but when I try to specify an integer for the levels argument, like this: 但是当我尝试为levels参数指定一个整数时,如下所示:

plt.contourf(x, y, z, levels=100)

it always returns the error: TypeError: len() of unsized object 它总是返回错误: TypeError: len() of unsized object

In the documentation, it says that the argument levels can be either int or array_like so I don't know why it would even call the len() function 在文档中,它说参数levels可以是intarray_like所以我不知道为什么它甚至会调用len()函数

Any ideas why this happens and any suggestion on how to fix it? 任何想法为什么会发生这种情况以及如何解决它的任何建议?

Sorry, this happens to you. 对不起,这件事发生在你身上。 The documentation changed in version 2.2.3 without this feature being fully implemented. 2.2.3版本中的文档已更改,但未完全实现此功能。 So depending on the version of matplotlib the levels argument is interpreted differently. 因此,根据matplotlib的版本, levels参数的解释方式不同。

matplotlib < 3.0.0 matplotlib <3.0.0

levels is interpreted as a list of levels where to draw contours. levels被解释为绘制轮廓的级别列表。 An integer is interpreted as a single level. 整数被解释为单个级别。 For a contourf (a filled contour) plot you need at least two levels. 对于contourf (填充轮廓)图,您至少需要两个级别。 Use the previously known way to specfify the number of levels as the second or fourth unnamed argument 使用先前已知的方法将级别数指定为第二个或第四个未命名参数

plt.contourf(z, 100)
plt.contourf(x, y, z, 100)

matplotlib >= 3.0.0 matplotlib> = 3.0.0

levels can take either a list or an integer. levels可以采用列表或整数。 When an integer, it signifies the (approximate [ * ]) number of levels. 当一个整数时,它表示(近似[ * ])级别。 The relevant PR is this . 相关的PR就是这个

plt.contourf(z, levels=100)
plt.contourf(x, y, z, levels=100)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM