简体   繁体   English

消除Matplotlib / Basemap pcolor图中的白边

[英]Eliminate white edges in Matplotlib/Basemap pcolor plot

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)

This results in this image: 这导致此图像: 在此输入图像描述

As you can see, there are white gaps between the grid cells. 如您所见,网格单元之间存在白色间隙。 How can I get rid of them? 我怎么能摆脱他们?

I know this is a an old question but I thought i would add my solution to this problem. 我知道这是一个老问题,但我想我会为这个问题添加我的解决方案。 I found your question when I was having the exact same problem as yours, ie a white line in my plot and a grid going from -180 to 180. The solution for me was to use the Basemap function addcyclic 当我遇到与你的问题完全相同的问题时,我发现了你的问题,即我的情节中的白线和从-180到180的网格。我的解决方案是使用底图函数addcyclic

from mpl_toolkits.basemap import Basemap, shiftgrid, addcyclic
SSTcyclic, lonCMIP5cyclic = addcyclic(SST, lonCMIP5)

This solved my problem. 这解决了我的问题。 Cheers, Trond 干杯,特隆德

I had the same problem once. 我曾经遇到过同样的问题。 It's very likely the problem is in longitude . 这个问题很可能是longitude问题。 Make sure 0 and 360 both exist in the input. 确保输入中存在0360 If not, manually add them, and make change to the mydata accordingly so that they have the same shape. 如果没有,请手动添加它们,并相应地更改mydata ,使它们具有相同的形状。

It looks like to me like the original post is not actually asking about the white area between 0 and 360 degrees. 在我看来,原来的帖子实际上并没有询问0360度之间的白色区域。

I think the OP is talking about the lines between each square of colour which would be consistent with this bug : 我认为OP正在讨论每个颜色方块之间的线条,这与这个bug是一致的:

It seems that savig a pcolor plot to a pdf format always includes gridlines, which isn't true for other output formats like png 看来,pdf格式的pcolor图总是包含网格线,但对于像png这样的其他输出格式则不然

Here is what the developers say about the problem: 以下是开发人员对此问题的看法:

I see gridlines in the resulting image in gs, xpdf Preview.app but not in Adobe Reader. 我在gs,xpdf Preview.app中看到结果图像中的网格线,但在Adobe Reader中没有。 When I zoom in Preview, the lines jump around a bit, and are always the same width on the screen regardless of zoom level. 当我放大预览时,线条会跳跃一点,并且无论缩放级别如何,屏幕上的宽度始终相同。

What gets drawn in this example is a lot of polygons, so that adjacent polygons share an edge with the exact same coordinates. 在这个例子中绘制的是很多多边形,因此相邻的多边形共享一个具有完全相同坐标的边。 The code fills the inside of each polygon, and apparently some rendering algorithms leave a minimal-width line between polygons. 代码填充每个多边形的内部,显然一些渲染算法在多边形之间留下最小宽度线。

So it's a problem with the PDF viewer, not with pcolor or any other aspect of matplotlib . 所以这是PDF查看器的问题,而不是pcolormatplotlib任何其他方面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM