gpt4 book ai didi

matplotlib - pcolormesh 的输入格式

转载 作者:行者123 更新时间:2023-12-02 20:44:38 28 4
gpt4 key购买 nike

我正在尝试使用 basemap 制作热量/强度图。我的输入是当时的一组纬度、经度和强度。数据集如下所示:

lat[0], lon[0] = intensity[0]
lat[1], lon[1] = intensity[1]
...
lat[n], lon[n] = intensity[n]

在每个索引处,纬度和经度对应于正确的传感器读数。我的代码看起来像这样:

fig = plt.figure(figsize=(10, 8))


# Set title
fig.suptitle("Intensities {} {}".format(start_time, stop_time))


# US Centered Map
map_axis = fig.add_subplot(111)
map = Basemap(
ax = map_axis,
lat_0 = 40, lon_0 = -95,
width = 6500e3, height = 6500e3,
projection = 'stere',
resolution = 'l'
)
map.drawcoastlines()


lats = ...
lons = ...
intn = ...


# Convert coordinates
lons, lats = map(lons, lats)


LONS, LATS = np.meshgrid(lons, lats)
map.pcolormesh(
LONS, LATS,
intn,
vmin = 0, vmax = 100
)


fig.savefig(file_name)
plt.close(fig)

此代码永远不会完成。我已经成功地自行绘制了 basemap 。 pcolormesh 是失败的。程序因此错误而崩溃。

    $ ./plot_intensities.py
Running 2013-04-10 00:02:30 2013-04-10 00:02:45
Traceback (most recent call last):
File "./plot_intensities.py", line 151, in <module>
make_maps(samples)
File "./plot_intensities.py", line 144, in make_maps
make_map(bin_samples, start, walk)
File "./plot_intensities.py", line 117, in make_map
vmin = 0, vmax = 100
File "/usr/lib/python3/dist-packages/mpl_toolkits/basemap/__init__.py", line 521, in with_transform
return plotfunc(self,x,y,data,*args,**kwargs)
File "/usr/lib/python3/dist-packages/mpl_toolkits/basemap/__init__.py", line 3418, in pcolormesh
ret = ax.pcolormesh(x,y,data,**kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/__init__.py", line 1814, in inner
return func(ax, *args, **kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py", line 5395, in pcolormesh
X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
File "/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py", line 4995, in _pcolorargs
numRows, numCols = C.shape
ValueError: not enough values to unpack (expected 2, got 1)

我了解到我的数据,第三个参数 intn 的格式不正确。我找不到任何关于如何制定该列表的文档。如何将其格式化为正确的形状?

谢谢。

最佳答案

如您所知,pcolormesh用于通过创建二维数组的伪彩色图来绘制四边形网格。错误详细信息确实表明:在 numRows, numCols = C.shape 行,它期望 C 是一个二维数组,而 CValueError:没有足够的值来解包(预期为2,得到1)来看,您提供的code>似乎是一个一维数组。在我看来,您引入的数据集似乎只有对角线上的强度值(其中lat == lon)。要获得颜色网格,您至少需要将强度数据扩展到二维数组并以某种方式填充缺失值。例如:

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np

fig = plt.figure(figsize=(10, 8))
# Set title
fig.suptitle("Intensities {} {}".format('start_time', 'stop_time'))
# US Centered Map
map_axis = fig.add_subplot(111)
map = Basemap(
ax = map_axis,
lat_0 = 40, lon_0 = -95,
width = 6500e3, height = 6500e3,
projection = 'stere',
resolution = 'l'
)
map.drawcoastlines()

# Tried my best to simulate your data example. Don't be surprise if the result is ugly ...
nstep = 1
lats = np.arange(map.latmin, map.latmax, nstep)
lons = np.arange(map.lonmin, map.lonmax, nstep)
l = min(len(lats), len(lons))
lats = lats[:l]
lons = lons[:l]
intn = np.random.randint(0, 100, size=l)

# Convert coordinates
lons, lats = map(lons, lats)
LONS, LATS = np.meshgrid(lons, lats)

# The following 3 lines are just an example of the minimum you got to do before it works.
intn_array = np.zeros(LONS.shape)
for i in range(l):
intn_array[i, i] = intn[i]
intn = intn_array

map.pcolormesh(
LONS, LATS,
intn_array,
vmin = 0, vmax = 100
)

plt.show()

enter image description here

关于matplotlib - pcolormesh 的输入格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44894328/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com