簡體   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