繁体   English   中英

Python:3个列表中的2D轮廓图,未在图中生成轴

[英]Python: 2D contour plot from 3 lists, axes not generated in plot

我有3个清单。 x,y和z。 我想创建一个等高线图,以点(x,y)的色标显示z的强度。

之前已经提出并回答了一个与此非常类似的问题( Python:来自3个列表的2d等高线图:x,y和rho? ),但是我遇到的问题是x和y轴不显示。

我的剧本:

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate

x =  [428, 598, 482, 351, 508, 413, 417, 471, 287, 578]
y =  [17449761, 19201380, 19766087, 18535270, 21441241, 20863875, 18686389, 17776179, 16372016, 20170943]
n =  [1.4406303782314329, 1.3248722314086339, 1.4064635429655712, 2.8806478042859767, 1.4067238073230157, 1.6444745940954972, 1.5180461138137205, 1.3819609357508074, 25.370740891787577, 1.3420941843768535]

# convert to arrays to make use of previous answer to similar question
x = np.asarray(x)
y = np.asarray(y)
z = np.asarray(n)
print "x = ", x
print "y = ", y
print "z = ", z

# Set up a regular grid of interpolation points
nInterp = 200
xi, yi = np.linspace(x.min(), x.max(), nInterp), np.linspace(y.min(), y.max(), nInterp)
xi, yi = np.meshgrid(xi, yi)

# Interpolate; there's also method='cubic' for 2-D data such as here
#rbf = scipy.interpolate.Rbf(x, y, z, function='linear')
#zi = rbf(xi, yi)
zi = scipy.interpolate.griddata((x, y), z, (xi, yi), method='linear')


plt.imshow(zi, vmin=z.min(), vmax=z.max(), origin='lower',
           extent=[x.min(), x.max(), y.min(), y.max()])

plt.xlabel("X")
plt.ylabel("Y")        
plt.colorbar()
plt.show()

这将生成以下图:

faulty_plot

我玩过Python中显示的Python脚本:来自3个列表的2d等高线图:x,y和rho? 插值点的数量以及原始列表/数组的大小似乎会导致轴消失/绘制点失败的问题。

我不知道是什么导致此错误。 任何帮助深表感谢。

正如已经解释这里imshow默认使用的纵横比1 在您的情况下,这会导致绘图比例降低。 imshow加入一条调整宽高比的语句(例如, aspect='auto' ),您将获得所需的绘图。

plt.imshow(zi, vmin=z.min(), vmax=z.max(), origin='lower',
       extent=[x.min(), x.max(), y.min(), y.max()], aspect='auto') 

结果是:

在此处输入图片说明

或者,您可能会发现使用tricontouring图这样很有趣:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as mtri

x =  [428, 598, 482, 351, 508, 413, 417, 471, 287, 578]
y =  [17449761, 19201380, 19766087, 18535270, 21441241, 20863875, 18686389, 17776179, 16372016, 20170943]
z =  [1.4406303782314329, 1.3248722314086339, 1.4064635429655712, 2.8806478042859767, 1.4067238073230157, 1.6444745940954972, 1.5180461138137205, 1.3819609357508074, 25.370740891787577, 1.3420941843768535]

x = np.asarray(x)
y = np.asarray(y)
z = np.asarray(z)

triang = mtri.Triangulation(x, y)
plt.triplot(triang)
plt.tricontourf(triang, z, vmin=z.min(), vmax=z.max(), origin='lower',
       extent=[x.min(), x.max(), y.min(), y.max()])

plt.xlabel("X")
plt.ylabel("Y")        
plt.colorbar()
plt.show()

三轮廓

另请参阅tri模块中的示例: http : //matplotlib.org/api/tri_api.html

暂无
暂无

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

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