使用pcolormesh时如何通过插值平滑?

时间:2016-06-14 22:07:42

标签: python matplotlib interpolation matplotlib-basemap

我有一个世界的底图,它使用pcolormesh填充数据(lintrends_mean)。因为数据有相对较大的网格框,我想平滑情节。但是,我无法弄清楚如何做到这一点。在绘图功能中设置着色='gouraud'会模糊网格框的边缘,但我希望看起来比这更漂亮,因为数据仍然会出现斑点。

这里有一个类似的问题给出答案,但我不明白答案,特别是“newdepth”的来源。由于我的声誉不足,我无法对其进行澄清澄清。 interpolation with matplotlib pcolor

#Set cmap properties
bounds = np.array([0.1,0.2,0.5,1,2,3,4,6,9,13,20,35,50])
norm = colors.LogNorm(vmin=0.01,vmax=55) #creates logarithmic scale
#cmap.set_under('#000099') # I want to use this- edit in Paint
cmap.set_over('#660000')  # everything above range of colormap

fig = plt.figure(figsize=(15.,10.))                     #create figure & size
m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90,llcrnrlon=0,urcrnrlon=360.,lon_0=180.,resolution='c') #create basemap & specify data area & res
m.drawcoastlines(linewidth=1)
m.drawcountries(linewidth=1)
m.drawparallels(np.arange(-90,90,30.),linewidth=0.3)
m.drawmeridians(np.arange(-180.,180.,90.),linewidth=0.3)            
meshlon,meshlat = np.meshgrid(lon,lat)                           #meshgrid turns lats & lons into 2D arrays
x,y = m(meshlon,meshlat)                                         #assign 2D arrays to new variables
trend = m.pcolormesh(x,y,lintrends_mean,cmap=plt.get_cmap('jet'),norm=norm) #plot the data & specify colormap & color range
cbar=m.colorbar(trend,size="3%", label='Linear Trend (mm/day/decade)',ticks=bounds,extend="max")
cbar.set_ticklabels(bounds)
plt.title('Linear Trends of PR (CanESM2 1979-2014)',fontsize=16)
plt.xlabel('Longitude',fontsize=10)
plt.ylabel('Latitude',fontsize=10)
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:10)

你有一些变种:

  1. pcolormesh使用特殊着色。
  2. 使用允许插值数据的imshow
  3. 使用scipy.interpolate插入数据并使用pcolormesh绘图。
  4. 看一下例子:

    import matplotlib.pylab as plt
    import numpy as np
    from scipy.interpolate import interp2d
    
    data = np.random.random((30,30))
    X = np.arange(0, 30, 1)
    Y = np.arange(0, 30, 1)
    X, Y = np.meshgrid(X, Y)
    
    # colormesh original
    plt.subplot(3, 2, 1)
    plt.pcolormesh(X, Y, data, cmap='RdBu')
    
    # pcolormesh with special shading
    plt.subplot(3, 2, 2)
    plt.pcolormesh(X, Y, data, cmap='RdBu',shading='gouraud')
    
    # imshow bilinear interp.
    plt.subplot(3, 2, 3)
    plt.imshow(data, cmap='RdBu', interpolation = 'bilinear')
    
    # imshow bicubic interp.
    plt.subplot(3, 2, 4)
    plt.imshow(data, cmap='RdBu', interpolation = 'bicubic')
    
    # scipy interp. cubic
    f = interp2d(X, Y, data, kind='cubic')
    xnew = np.arange(0, 30, .1)
    ynew = np.arange(0, 30, .1)
    data1 = f(xnew,ynew)
    Xn, Yn = np.meshgrid(xnew, ynew)
    plt.subplot(3, 2, 5)
    plt.pcolormesh(Xn, Yn, data1, cmap='RdBu')
    
    plt.show()
    

    enter image description here