我想使用matplotlib使用我的数据绘制轮廓,但它不起作用。我尝试了很多方法,但问题仍然一再被提出。 这是我的代码:
x = list(map(eval, Lon))
y = list(map(eval, Lat))
z = list(map(eval, z)) # convert the data to numbers
xi = np.linspace(22, 23, 99)
yi = np.linspace(113, 115, 99)
xi, yi = np.meshgrid(xi, yi)
zi = griddata(x, y, z, xi, yi)
fig, ax = plt.subplots(figsize=(10, 5))
im = ax.contour(xi, yi, zi, linewidths = 0.5, colors = 'k')
im = ax.contourf(xi, yi, zi)
ax.scatter(Lon, Lat, c=z, s=50,
vmin=zi.min(), vmax=zi.max())
fig.colorbar(im)
plt.xticks()
plt.yticks()
plt.show()
这是我的x,y和z:
z = [116.3, 100.9, 98.2, 95.1, 94.4, 94.3, 91.6, 90.9, 88.5, 86.8, 86.2, 86.1, 84.2, 83.5, 80.8, 80.1, 78.1, 76.2, 74.9, 74.3, 73.7, 73.3, 72.4, 71.5, 71.4, 70.4, 67.9, 65.1, 63.3, 62.7, 61.6, 61.5, 59.5, 59.3, 58.9, 57.1, 56, 56, 54.9, 54.9, 54, 53.8, 52.6, 49.9, 49.8, 48.4, 48.4, 47.2, 46, 45.7, 45.2, 44.6, 42.6, 42, 41.9, 40.3, 40.1, 39.4, 39.3, 39.2, 37.2, 37.1, 37, 36.9, 36.7, 36.2, 34.7, 34.4, 34.2, 33.6, 32.1, 32, 31.9, 30.7, 30.7, 30.6, 30.4, 29.8, 29.5, 29, 28.3, 28.2, 28.1, 26.7, 24.9, 24.5, 22.9, 21.9, 20.3, 18.8, 17.7, 16.7, 13.6, 10.6, 9.6, 7.4, 4.8, 0, 0]
y = [113.9088889, 113.8797222, 113.9469444, 114.0491667, 114.0163889, 114.0708333, 114.1038889, 113.9447222, 113.9308333, 114.0094444, 113.8438889, 114.0141667, 114.0955556, 114.1119444, 114.1133333, 114.0141667, 113.9202778, 113.9061111, 114.1047222, 113.8930556, 114.0047222, 113.8686111, 113.9691667, 114.0025, 114.07, 113.8855556, 114.1794444, 114.5352778, 114.0291667, 114.1011111, 113.8372222, 114.0291667, 114.1694444, 114.195, 114.3997222, 113.9338889, 113.8941667, 113.8936111, 113.8622222, 114.1508333, 114.3625, 114.1488889, 113.8175, 114.1933333, 113.9680556, 113.9544444, 114.2236111, 114.3405556, 114.3002778, 114.5144444, 113.9658333, 113.8197222, 113.8011111, 113.8980556, 114.2622222, 114.415, 113.8788889, 114.3663889, 113.9347222, 113.9536111, 114.0397222, 114.4319444, 113.7883333, 114.2338889, 114.1266667, 114.1122222, 113.7672222, 114.2422222, 114.1230556, 114.1658333, 114.1508333, 114.2361111, 114.3044444, 114.4844444, 113.8736111, 113.8602778, 114.2238889, 114.47, 114.4861111, 113.8361111, 114.2430556, 114.4786111, 114.1436111, 114.0822222, 114.3922222, 114.3561111, 114.2975, 114.3552778, 114.5505556, 114.5263889, 113.8911111, 114.5002778, 114.2672222, 113.9147222, 114.3372222, 114.2752778, 114.1880556, 113.9727778, 114.3247222]
x = [22.48, 22.55277778, 22.55111111, 22.525, 22.53027778, 22.56861111, 22.54722222, 22.50388889, 22.48861111, 22.56055556, 22.56055556, 22.54888889, 22.5375, 22.55916667, 22.53083333, 22.65888889, 22.53, 22.56277778, 22.54972222, 22.58861111, 22.54138889, 22.495, 22.53916667, 22.68305556, 22.63083333, 22.47, 22.66222222, 22.56194444, 22.62305556, 22.57194444, 22.60916667, 22.5725, 22.63694444, 22.67944444, 22.69583333, 22.605, 22.65361111, 22.65388889, 22.66722222, 22.65611111, 22.67166667, 22.61638889, 22.67416667, 22.64638889, 22.62055556, 22.79472222, 22.64638889, 22.69444444, 22.67888889, 22.64555556, 22.735, 22.72833333, 22.66083333, 22.70472222, 22.76694444, 22.63361111, 22.74388889, 22.745, 22.68472222, 22.7625, 22.70777778, 22.61888889, 22.69305556, 22.68527778, 22.55083333, 22.60555556, 22.72333333, 22.72416667, 22.69611111, 22.56472222, 22.55166667, 22.76583333, 22.7775, 22.53444444, 22.83694444, 22.80166667, 22.555, 22.60055556, 22.48277778, 22.77916667, 22.58388889, 22.54166667, 22.57166667, 22.72722222, 22.615, 22.61, 22.60083333, 22.7125, 22.59694444, 22.48138889, 22.78166667, 22.56944444, 22.60083333, 22.5475, 22.59527778, 22.56666667, 22.595, 22.59638889, 22.60555556]
我的错误是:
C:\Users\15432\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\contour.py:1514: UserWarning: Warning: converting a masked element to nan.
self.zmax = float(z.max())
C:\Users\15432\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\contour.py:1515: UserWarning: Warning: converting a masked element to nan.
self.zmin = float(z.min())
C:\Users\15432\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\contour.py:1153: RuntimeWarning: invalid value encountered in greater
return lev[(lev > zmin) & (lev < zmax)]
C:\Users\15432\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\contour.py:1153: RuntimeWarning: invalid value encountered in less
return lev[(lev > zmin) & (lev < zmax)]
Traceback (most recent call last):
File "D:\Python\meteotest.py", line 43, in <module>
im = ax.contour(xi, yi, zi, linewidths = 0.5, colors = 'k')
File "C:\Users\15432\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\__init__.py", line 1898, in inner
return func(ax, *args, **kwargs)
File "C:\Users\15432\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axes\_axes.py", line 5825, in contour
contours = mcontour.QuadContourSet(self, *args, **kwargs)
File "C:\Users\15432\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\contour.py", line 865, in __init__
self._process_levels()
File "C:\Users\15432\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\contour.py", line 1199, in _process_levels
self.vmin = np.amin(self.levels)
File "C:\Users\15432\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\fromnumeric.py", line 2372, in amin
out=out, **kwargs)
File "C:\Users\15432\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\_methods.py", line 29, in _amin
return umr_minimum(a, axis, None, out, keepdims)
ValueError: zero-size array to reduction operation minimum which has no identity
这让我困扰了几天。我已成功运行一个小程序,我手动输入数据,但要从txt文档中理解它,似乎不可能成功。任何人都可以帮助我吗?
答案 0 :(得分:0)
一如既往,阅读文档会有所帮助。 scipy.interpolate.griddata
需要3个参数
scipy.interpolate.griddata(points, values, xi, method='linear', ...)
所以你只需要坚持文档并提供这3个参数:
zi = griddata(points=(x, y), values=z, xi=(xi, yi))
完整代码:
from scipy.interpolate import griddata
import numpy as np
import matplotlib.pyplot as plt
z = [116.3, 100.9, 98.2, 95.1, 94.4, 94.3, 91.6, 90.9, 88.5, 86.8, 86.2, 86.1, 84.2, 83.5, 80.8, 80.1, 78.1, 76.2, 74.9, 74.3, 73.7, 73.3, 72.4, 71.5, 71.4, 70.4, 67.9, 65.1, 63.3, 62.7, 61.6, 61.5, 59.5, 59.3, 58.9, 57.1, 56, 56, 54.9, 54.9, 54, 53.8, 52.6, 49.9, 49.8, 48.4, 48.4, 47.2, 46, 45.7, 45.2, 44.6, 42.6, 42, 41.9, 40.3, 40.1, 39.4, 39.3, 39.2, 37.2, 37.1, 37, 36.9, 36.7, 36.2, 34.7, 34.4, 34.2, 33.6, 32.1, 32, 31.9, 30.7, 30.7, 30.6, 30.4, 29.8, 29.5, 29, 28.3, 28.2, 28.1, 26.7, 24.9, 24.5, 22.9, 21.9, 20.3, 18.8, 17.7, 16.7, 13.6, 10.6, 9.6, 7.4, 4.8, 0, 0]
y = [113.9088889, 113.8797222, 113.9469444, 114.0491667, 114.0163889, 114.0708333, 114.1038889, 113.9447222, 113.9308333, 114.0094444, 113.8438889, 114.0141667, 114.0955556, 114.1119444, 114.1133333, 114.0141667, 113.9202778, 113.9061111, 114.1047222, 113.8930556, 114.0047222, 113.8686111, 113.9691667, 114.0025, 114.07, 113.8855556, 114.1794444, 114.5352778, 114.0291667, 114.1011111, 113.8372222, 114.0291667, 114.1694444, 114.195, 114.3997222, 113.9338889, 113.8941667, 113.8936111, 113.8622222, 114.1508333, 114.3625, 114.1488889, 113.8175, 114.1933333, 113.9680556, 113.9544444, 114.2236111, 114.3405556, 114.3002778, 114.5144444, 113.9658333, 113.8197222, 113.8011111, 113.8980556, 114.2622222, 114.415, 113.8788889, 114.3663889, 113.9347222, 113.9536111, 114.0397222, 114.4319444, 113.7883333, 114.2338889, 114.1266667, 114.1122222, 113.7672222, 114.2422222, 114.1230556, 114.1658333, 114.1508333, 114.2361111, 114.3044444, 114.4844444, 113.8736111, 113.8602778, 114.2238889, 114.47, 114.4861111, 113.8361111, 114.2430556, 114.4786111, 114.1436111, 114.0822222, 114.3922222, 114.3561111, 114.2975, 114.3552778, 114.5505556, 114.5263889, 113.8911111, 114.5002778, 114.2672222, 113.9147222, 114.3372222, 114.2752778, 114.1880556, 113.9727778, 114.3247222]
x = [22.48, 22.55277778, 22.55111111, 22.525, 22.53027778, 22.56861111, 22.54722222, 22.50388889, 22.48861111, 22.56055556, 22.56055556, 22.54888889, 22.5375, 22.55916667, 22.53083333, 22.65888889, 22.53, 22.56277778, 22.54972222, 22.58861111, 22.54138889, 22.495, 22.53916667, 22.68305556, 22.63083333, 22.47, 22.66222222, 22.56194444, 22.62305556, 22.57194444, 22.60916667, 22.5725, 22.63694444, 22.67944444, 22.69583333, 22.605, 22.65361111, 22.65388889, 22.66722222, 22.65611111, 22.67166667, 22.61638889, 22.67416667, 22.64638889, 22.62055556, 22.79472222, 22.64638889, 22.69444444, 22.67888889, 22.64555556, 22.735, 22.72833333, 22.66083333, 22.70472222, 22.76694444, 22.63361111, 22.74388889, 22.745, 22.68472222, 22.7625, 22.70777778, 22.61888889, 22.69305556, 22.68527778, 22.55083333, 22.60555556, 22.72333333, 22.72416667, 22.69611111, 22.56472222, 22.55166667, 22.76583333, 22.7775, 22.53444444, 22.83694444, 22.80166667, 22.555, 22.60055556, 22.48277778, 22.77916667, 22.58388889, 22.54166667, 22.57166667, 22.72722222, 22.615, 22.61, 22.60083333, 22.7125, 22.59694444, 22.48138889, 22.78166667, 22.56944444, 22.60083333, 22.5475, 22.59527778, 22.56666667, 22.595, 22.59638889, 22.60555556]
xi = np.linspace(22, 23, 99)
yi = np.linspace(113, 115, 99)
xi, yi = np.meshgrid(xi, yi)
zi = griddata(points=(x, y), values=z, xi=(xi, yi))
fig, ax = plt.subplots(figsize=(10, 5))
im = ax.contour(xi, yi, zi, linewidths = 0.5, colors = 'k')
im = ax.contourf(xi, yi, zi)
ax.scatter(x, y, c=z, s=50,
vmin=zi.min(), vmax=zi.max())
fig.colorbar(im)
plt.xticks()
plt.yticks()
plt.show()
产生