![](/img/trans.png)
[英]Python - error in drawing contour plot using scipy.integrate.quad
[英]Python: Type error when drawing contour plot
盡管使用了搜索功能,但我一直找不到答案。 我有兩個假設,但不知道在多大程度上適用。 現在的問題是:
我想畫一個輪廓。 為此,我在這里獲得了以下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行,位於新的 _data = np.array(data,dtype = dtype,copy = copy,subok = True, ndmin = ndmin)ValueError:設置具有序列的數組元素。
現在假設是什么可能導致此問題:
要么
就像@rocksportrocker所暗示的那樣,您需要考慮到histogram2d
還會返回histogram2d
之外的邊。 另一個細節是您可能希望顯式傳遞一個范圍,否則將根據數據中的實際最小和最大值為您選擇一個范圍。 然后,您想將邊緣轉換為該圖的像元中心。 像這樣:
import numpy as np
import matplotlib.pyplot as plt
n = 1000000 # how many data points
xmin, xmax = 0.0, 54.0 # distances
ymin, ymax = 0.0, 180.0 # angles
# make up some random data
xi=np.random.normal(xmax/2.0, xmax/4.0, n)
yi=np.random.normal(ymax/3.0, ymax/3.0, n)
Z, xedges, yedges = np.histogram2d(xi,yi, bins=(270,90), range=[[xmin, xmax], [ymin, ymax]])
# find the cell centers from the cell edges
x = 0.5*(xedges[:-1] + xedges[1:])
y = 0.5*(yedges[:-1] + yedges[1:])
# promote to 2D arrays
Y, X = np.meshgrid(y, x)
plt.contour(X,Y,Z)
plt.ylabel('angles')
plt.xlabel('distance')
plt.colorbar()
plt.savefig("hist2d.png")
得出這樣的國家圖:
但我個人在這種情況下不會使用輪廓,因為直方圖可能會很吵。
您的回溯表明該錯誤不會從調用matplotlib引發,而是numpy引發ValueError。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.