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.
UPDATE: The issue didn't occur after installing the version >=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:
plt.contourf(x, y, z)
but when I try to specify an integer for the levels argument, like this:
plt.contourf(x, y, z, levels=100)
it always returns the error: 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
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. So depending on the version of matplotlib the levels
argument is interpreted differently.
matplotlib < 3.0.0
levels
is interpreted as a list of levels where to draw contours. An integer is interpreted as a single level. For a contourf
(a filled contour) plot you need at least two levels. 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
levels
can take either a list or an integer. When an integer, it signifies the (approximate [ * ]) number of levels. The relevant PR is this .
plt.contourf(z, levels=100)
plt.contourf(x, y, z, levels=100)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.