【问题标题】:Tilted grid network plotting in Basemap底图中的倾斜网格网络绘图
【发布时间】:2016-06-18 07:00:01
【问题描述】:

这是一个示例图作为说明。

该图显示了欧洲部分地区的卫星 SO2 列数据。

由于卫星与经度的差异,符合卫星扫描原理的网格网络与经度不平行。

不知道是否可以在matplotlib.basemap中使用pcolor或者pcolormesh来绘制这种网格网络。所以,我在这里发布我的问题。

【问题讨论】:

    标签: python matplotlib matplotlib-basemap


    【解决方案1】:

    我偶然发现了这个问题,因为我也在寻找一种在地图上绘制网格卫星测量值的方法,使用 matplotlibbasemap
    我不确定我的想法是否与您的问题相关,因为我的像素只能假设非常有限的离散值(4),但我还是决定回答,看看您是否最终找到了一个有效的解决方案。我所做的是使用Polygon 方法在地图上直接将每个 单个像素绘制为多边形。 我将alpha 值设置为基础物理测量的函数。在我的例子中——一个云罩图——这个策略效果很好。
    这是为每个要绘制的像素调用的函数:

    def draw_cloud_pixel(lats, lons, index, mapplot):
        """Draw a pixel on the map. The fill color alpha level depends on the cloud index, 
        ranging from 0.1 (almost fully transparent) for confidently clear pixels to 1 (fully opaque)
        for confidently cloudy pixels.
    
        Keyword arguments:
        lats -- Array of latitude values for the pixel 4 corner points (numpy array)
        lons -- Array of longitudes values for the pixel 4 corner points (numpy array)
        index -- Cloud mask index for given pixel: 
            0: confidently_cloudy
            1: probably_cloudy
            2: probably_clear
            3: confidently_clear
        mapplot -- Map object for coordinate transformation
    
        Returns:
        None
        """
        x, y = mapplot(lons, lats)
        xy = zip(x,y)
        poly = Polygon(xy, facecolor='white', alpha=1-0.3*index)
        plt.gca().add_patch(poly)
    

    在我的主要绘图例程中,然后我为所选区域中的每个像素调用 draw_cloud_pixel 函数:

    # draw plot, each pixel at the time
    for scanline in xrange(select_cp_lat.shape[0]):
        for pixel in xrange(select_cp_lat.shape[1]):
            draw_cloud_pixel(select_cp_lat[scanline, pixel,:], 
                             select_cp_lon[scanline, pixel,:], 
                             cloud_mask[scanline, pixel], 
                             mapplot)
    

    我得到这样的情节:

    【讨论】:

      【解决方案2】:

      查看此页面中的不同示例:http://www.uvm.edu/~jbagrow/dsv/heatmap_basemap.html

      样本的主要思想是在basemap 上绘制pcolormesh

      import csv
      import numpy as np
      from mpl_toolkits.basemap import Basemap
      import matplotlib.pyplot as plt
      from matplotlib.colors import LinearSegmentedColormap
      
      # load earthquake epicenters:
      # http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_week.csv
      lats, lons = [], []
      with open('earthquake_data.csv') as f:
          reader = csv.reader(f)
          next(reader) # Ignore the header row.
          for row in reader:
              lat = float(row[1])
              lon = float(row[2])
              # filter lat,lons to (approximate) map view:
              if -130 <= lon <= -100 and 25 <= lat <= 55:
                  lats.append( lat )
                  lons.append( lon )
      
      # Use orthographic projection centered on California with corners
      # defined by number of meters from center position:
      m  = Basemap(projection='ortho',lon_0=-119,lat_0=37,resolution='l',\
                   llcrnrx=-1000*1000,llcrnry=-1000*1000,
                   urcrnrx=+1150*1000,urcrnry=+1700*1000)
      m.drawcoastlines()
      m.drawcountries()
      m.drawstates()
      
      # ######################################################################
      # bin the epicenters (adapted from 
      # http://stackoverflow.com/questions/11507575/basemap-and-density-plots)
      
      # compute appropriate bins to chop up the data:
      db = 1 # bin padding
      lon_bins = np.linspace(min(lons)-db, max(lons)+db, 10+1) # 10 bins
      lat_bins = np.linspace(min(lats)-db, max(lats)+db, 13+1) # 13 bins
      
      density, _, _ = np.histogram2d(lats, lons, [lat_bins, lon_bins])
      
      # Turn the lon/lat of the bins into 2 dimensional arrays ready
      # for conversion into projected coordinates
      lon_bins_2d, lat_bins_2d = np.meshgrid(lon_bins, lat_bins)
      
      # convert the bin mesh to map coordinates:
      xs, ys = m(lon_bins_2d, lat_bins_2d) # will be plotted using pcolormesh
      # ######################################################################
      
      # define custom colormap, white -> nicered, #E6072A = RGB(0.9,0.03,0.16)
      cdict = {'red':  ( (0.0,  1.0,  1.0),
                         (1.0,  0.9,  1.0) ),
               'green':( (0.0,  1.0,  1.0),
                         (1.0,  0.03, 0.0) ),
               'blue': ( (0.0,  1.0,  1.0),
                         (1.0,  0.16, 0.0) ) }
      custom_map = LinearSegmentedColormap('custom_map', cdict)
      plt.register_cmap(cmap=custom_map)
      
      # add histogram squares and a corresponding colorbar to the map:
      plt.pcolormesh(xs, ys, density, cmap="custom_map")
      
      cbar = plt.colorbar(orientation='horizontal', shrink=0.625, aspect=20, fraction=0.2,pad=0.02)
      cbar.set_label('Number of earthquakes',size=18)
      #plt.clim([0,100])
      
      # translucent blue scatter plot of epicenters above histogram:    
      x,y = m(lons, lats)
      m.plot(x, y, 'o', markersize=5,zorder=6, markerfacecolor='#424FA4',markeredgecolor="none", alpha=0.33)
      
      
      # http://matplotlib.org/basemap/api/basemap_api.html#mpl_toolkits.basemap.Basemap.drawmapscale
      m.drawmapscale(-119-6, 37-7.2, -119-6, 37-7.2, 500, barstyle='fancy', yoffset=20000)
      
      # make image bigger:
      plt.gcf().set_size_inches(15,15)
      
      plt.show()
      

      【讨论】:

        猜你喜欢
        • 2018-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-03
        • 2019-05-13
        • 2016-12-25
        • 2016-12-23
        • 1970-01-01
        相关资源
        最近更新 更多