【问题标题】:How to plot pcolormesh for specific coordinate points instead of a meshgrid from 1D lat lon values如何从一维纬度值绘制特定坐标点的 pcolormesh 而不是网格网格
【发布时间】:2019-09-23 14:32:49
【问题描述】:

我有一个包含三列的数据框:Latitude、Longitude 和 Variable。其中大约有 100K 行。我需要绘制这些数据的颜色网格,显示奇怪的线条和区域。

我在各处都看到过使用网格网格转换一维纬度数组的帖子,但在这种情况下这不起作用,因为它们本身就是特定的坐标。我试图按升序对它们进行排序 lon 和/或 lat 和/或两者。然后我尝试移动坐标,将它们重新塑造成它们总长度的因子,但徒劳无功。

数据:

    Longitude = [-10, -40, 34,  12, 67, ...]  # 138627 elements
    Latitude  = [ 23, -89, 67, -25, 92, ...]  # same
    Variable  = [  1,   2,  3,   4,  5, ...]  # same

代码:

    import cartopy, glob, warnings, os, matplotlib.pyplot as plt
    import numpy as np, cartopy.crs as ccrs
    from   netCDF4 import Dataset as netcdf_dataset
    from   cartopy import config
    from mpl_toolkits.basemap import Basemap
    warnings.simplefilter('ignore')

    # CARTOPY
    ax      = plt.axes(projection=ccrs.PlateCarree())
    plt.pcolormesh(Longitude, Latitude, Variable)
    ax.coastlines()
    plt.show()

    # BASEMAP
    m = Basemap(projection='cyl', llcrnrlat=-90, llcrnrlon=-180,  rcrnrlat=90, urcrnrlon=180)

    #TRY 1
    new_coor = sorted([(i,j) for i,j in zip(Longitude, Latitude)], key=lambda x: x[0])
    shape     = (3,46209)  #len(Latitude) = 138627
    Longitude = np.asarray([i[0] for i in new_coor]).reshape(shape)
    Latitude  = np.asarray([i[1] for i in new_coor]).reshape(shape)
    xi, yi    = m(Longitude, Latitude)
    cs        = m.pcolor(xi, yi, np.squeeze(Variable)) #tried *pcolormesh* also
    # tried *m.shifting()* and *lat_lon = True* also
    m.drawcoastlines()
    m.drawcountries()

    cbar      = m.colorbar(cs, location='bottom', pad="10%")
    plt.show()

【问题讨论】:

标签: python matplotlib matplotlib-basemap cartopy


【解决方案1】:

您正在处理非结构化数据。您可能想定义一个grid 并将interpolate 数据放到这个网格上,但在我看来,更简洁的方法是使用tricontourf。此函数使用triangulation,因此您的原始数据在绘制之前不会被修改。有用的关键字有,例如,antialiased、levels、extend、cmap。

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from matplotlib.tri import Triangulation
triMesh = Triangulation(Longitude, Latitude)
fig, ax = plt.subplots(nrows=1, ncols=1, num=0,
                       subplot_kw={'projection': ccrs.PlateCarree()},
                       figsize=(16, 8))
ctrf = ax.tricontourf(triMesh, Variable)
cbar = fig.colorbar(ctrf)

【讨论】:

  • 感谢您的建议。不幸的是,它没有用。我仍然让那些奇怪的区域充满了线条。 @Patol75
  • 如果您实际显示了绘制的图形,将会有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多