pcolormesh 的输入格式 [英] Input Format to pcolormesh

查看:66
本文介绍了pcolormesh 的输入格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

lat[0], lon[0] = 强度[0]lat [1],lon [1] =强度[1]...lat [n],lon [n] =强度[n]

在每个索引处,纬度和经度对应于正确的传感器读数.我的代码如下所示:

  fig = plt.figure(figsize =(10,8))#设置标题fig.suptitle("Intensities {} {}".format(start_time,stop_time))#美国中心地图map_axis = fig.add_subplot(111)地图=底图(ax = map_axis,lat_0 = 40,lon_0 = -95,宽度 = 6500e3,高度 = 6500e3,投影 = '立体',分辨率='l')map.drawcoastlines()拉特 = ...隆 = ...intn = ...#转换坐标lons,lats = map(lons,lats)LONS, LATS = np.meshgrid(lons, lats)地图.pcolormesh(LONS,LATS,内部,vmin = 0,vmax = 100)fig.savefig(file_name)plt.close(图)

此代码永远不会完成.我已经成功地自己绘制了底图.pcolormesh 是失败的原因.程序因该错误而崩溃.

 $ ./plot_intensities.py运行 2013-04-10 00:02:30 2013-04-10 00:02:45回溯(最近一次调用最后一次):文件./plot_intensities.py",第 151 行,在 <module> 中make_maps(样本)文件./plot_intensities.py",第 144 行,在 make_maps 中make_map(bin_samples,开始,步行)在make_map中的文件"./plot_intensities.py",第117行vmin = 0,vmax = 100文件"/usr/lib/python3/dist-packages/mpl_toolkits/basemap/__init__.py",第521行,with_transform返回 plotfunc(self,x,y,data,*args,**kwargs)文件/usr/lib/python3/dist-packages/mpl_toolkits/basemap/__init__.py",第 3418 行,在 pcolormesh 中ret = ax.pcolormesh(x,y,data,**kwargs)文件/usr/lib/python3/dist-packages/matplotlib/__init__.py",第1814行,在内部返回 func(ax, *args, **kwargs)文件/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py",第 5395 行,在 pcolormesh 中X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)_pcolorargs中的文件"/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py",行4995numRows,numCols = C.shapeValueError:没有足够的值来解包(预期为 2,得到 1)

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

谢谢.

解决方案

如您所知,

I'm attempting to make heat/intensity map using Basemap. My inputs are a set of lats, lons, and intensity at that point. The dataset looks like this:

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

At each index the lat and lon correspond to the correct sensor reading. My code looks something like this:

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) 

This code never completes. I've successfully plotted the Basemap by itself. The pcolormesh is what is failing. The program crashes with this error.

    $ ./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)

I understand that my data, the third argument intn is not formatted correctly. I cannot find any documentation as to how I should shape that list. How do I format it to the correct shape?

Thanks.

解决方案

As you know, pcolormesh is used to plot a quadrilateral mesh by creating a pseudocolor plot of a 2-D array. The error details indeed indicated that: at line numRows, numCols = C.shape, it expect C to be a 2-D array, while the C you provided seems to be a 1-D array, judging from ValueError: not enough values to unpack (expected 2, got 1). The dataset you introduced seems to me having only intensity values on the diagonal (where lat == lon). To get a colormesh, you need to at least extend intensity data into 2-D array and somehow fill in missing values. For example:

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()

这篇关于pcolormesh 的输入格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆