消除Matplotlib/Basemap pcolor图中的白色边缘 [英] Eliminate white edges in Matplotlib/Basemap pcolor plot
本文介绍了消除Matplotlib/Basemap pcolor图中的白色边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码在地图上绘制数据:
I am plotting data on a map using this code:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
from scipy.io import netcdf
ncfile = netcdf.netcdf_file(myfile.nc,'r')
lon = ncfile.variables['longitude'][:]
lat = ncfile.variables['latitude'][:]
data = ncfile.variables['mydata'][:]
ncfile.close()
m = Basemap(projection='nplaea', boundinglat=40, lon_0=270)
m.drawcoastlines(linewidth=.6, zorder=2)
m.drawparallels(np.arange(-80.,81.,20.), zorder=1)
m.drawmeridians(np.arange(-180.,181.,20.), zorder=1)
cNorm = mpl.colors.Normalize(vmin=0, vmax=np.nanmax(data))
cmap = plt.get_cmap('jet')
lons, lats = np.meshgrid(lon, lat)
x, y = m(lons, lats)
datamap = m.pcolor(x, y, data, zorder=0)
datamap.set_norm(cNorm)
plt.colorbar(datamap, cmap=cmap, norm=cNorm, shrink=0.5)
plt.savefig('figures/map_polar.png', dpi=150, bbox_inches='tight', pad_inches=0.4)
这将产生此图像:
如您所见,网格单元之间存在白色间隙.我该如何摆脱它们?
As you can see, there are white gaps between the grid cells. How can I get rid of them?
推荐答案
我曾经遇到过同样的问题.问题很有可能出在longitude
中.
确保输入中同时存在0
和360
.如果没有,请手动添加它们,然后
相应地对mydata
进行更改,以使它们具有相同的形状.
I had the same problem once. It's very likely the problem is in longitude
.
Make sure 0
and 360
both exist in the input. If not, manually add them, and
make change to the mydata
accordingly so that they have the same shape.
这篇关于消除Matplotlib/Basemap pcolor图中的白色边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文