Python:绘制等值线图时出现类型错误
尽管使用了搜索功能,我还是无法找到答案。我有两个假设,但不知道它们可以应用到什么程度。现在的问题是:
我想绘制轮廓。为此,我有以下 python 代码:
import numpy as np
import matplotlib.pyplot as plt
xi=list_of_distance
yi=list_of_angle
x = np.arange(0,54,0.2)
y = np.arange(0,180,2.0)
Z = np.histogram2d(xi,yi,bins=(274,90))
X, Y = np.meshgrid(x, y)
plt.contour(X,Y,Z)
plt.ylabel('angles')
plt.xlabel('distance')
plt.colorbar()
plt.show()
xi 和 yi 是包含浮点值的列表。 x 和 y 定义“间隔”...例如: x 以 0.2 步长生成一个值从 0 到 54 的列表 y 在 2.0 步中生成一个值从 0 到 180 的列表,
ZI 使用 numpy 函数创建 2D 直方图。事实上,这似乎是造成麻烦的地方。
当调用函数 plt.contour(X,Y,Z) 时,出现以下错误消息:
...文件“/usr/lib/pymodules/python2.7/numpy/ma/core.py”,第 2641 行,new _data = np.array(数据, dtype=dtype, 复制=复制, subok=True, ndmin=ndmin) ValueError:使用序列设置数组元素。
现在假设可能导致此问题的原因:
- 它似乎需要一个数组,但它接收的不是一个 numpy 数组,而是一个列表
,或者
- 我们得到了比其他行短的行(我想到了这个想法,在一位同事跑完之后一年前就遇到过这样的问题 - 通过弄清楚最后一行比所有其他行短 2 个元素,已经解决了这个问题......)
despite using the search function I've been unable to find an answer. I got two assumptions, but don't know in how far they may apply. Now the problem:
I'd like to plot a contour. For this I've got here the following python code:
import numpy as np
import matplotlib.pyplot as plt
xi=list_of_distance
yi=list_of_angle
x = np.arange(0,54,0.2)
y = np.arange(0,180,2.0)
Z = np.histogram2d(xi,yi,bins=(274,90))
X, Y = np.meshgrid(x, y)
plt.contour(X,Y,Z)
plt.ylabel('angles')
plt.xlabel('distance')
plt.colorbar()
plt.show()
xi and yi are lists containing float values.
x and y are defining the 'intervals' ...for example:
x generates a list with values from 0 to 54 in 0.2 steps
y generates a list with values from 0 to 180 in 2.0 steps
with Z I make use of the numpy function to create 2D-Histograms. Actually this seems to be the spot that causes trouble.
When the function plt.contour(X,Y,Z) is called, the following error message emerges:
... File "/usr/lib/pymodules/python2.7/numpy/ma/core.py", line 2641, in new
_data = np.array(data, dtype=dtype, copy=copy, subok=True, ndmin=ndmin)
ValueError: setting an array element with a sequence.
Now to the assumptions what may cause this problem:
- It seems like it expects an array but instead of an numpy-array it receives a list
or
- We got a row that is shorter than the others (I came to that thought, after a collegue ran into such an issue a year ago - there it has been fixed by figuring out that the last row was by 2 elements shorter than all others...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 @rocksportrocker 所暗示的,您需要考虑
histogram2d
除了直方图之外还返回边缘。另一个细节是您可能希望显式传递一个范围,否则将根据数据中的实际最小值和最大值为您选择一个范围。然后,您需要将绘图的边缘转换为单元格中心。像这样的东西:产生这样的计数图:
但就我个人而言,在这种情况下我不会使用轮廓,因为直方图可能有噪音。
As @rocksportrocker implies, you need to take into account that
histogram2d
returns the edges in addition to the histogram. Another detail is that you probably want to explicitly pass in a range, otherwise one will be chosen for you based on the actual min and max values in your data. You then want to convert the edges to cell centers for the plot. Something like this:yields a countour plot like this:
but personally I wouldn't use contours in this case, since the histogram is likely to be noisy.
您的回溯表明错误不是由对 matplotlib 的调用引发的,而是 numpy 引发了 ValueError。
Your traceback indicates that the error does not raise from the call to matplotlib, it is numpy which raises the ValueError.