我正在使用matplotlib和Cartopy从二维网格数据集中生成图像。以下链接中的示例如下:
驱动此图像创建的关键代码如下:
dataset = Dataset('/pathtofile/' + ymd + '/file_d0tmax.nc')
temp = dataset.variables['TMAX2M'][:]
lats = dataset.variables['lat'][:]
lons = dataset.variables['lon'][:]
fig = plt.figure(figsize=(14,8))
ax = plt.axes([0.25, 0.05, 0.95, 0.9],projection=crs.LambertConformal())
ax.set_adjustable('datalim')
#WPTZ DMA
ax.set_extent([-74.890000, -70.890000, 42.680000, 45.380000], crs=crs.LambertConformal())
im = plt.contourf(lons, lats, temp, levels=vals, cmap=cmap, norm=norm, transform=crs.LambertConformal())
plt.colorbar(im, pad=0.05, ticks=[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100])
填充轮廓成功生成,但是在特定区域,填充轮廓出现锯齿或斑点。是否有比contourf()更好的替代方法来产生更平滑的插值?
答案 0 :(得分:1)
如果您想要真正的插值,则contourf()
最好,尤其是@ImportanceOfBeingErnest建议使用更多值时。但是,您可以尝试使用pcolormesh()
,它对每个点进行采样而不是在它们之间进行插值。我无法想象会更好。我会选择更多的值,以便您获得更平滑的插值。
答案 1 :(得分:1)
"jagged or splotchy"
I'm seeing little diamonds, which I guess are single points with a value significantly different from their neighbours.
In that context these contours are the "right" answer that respects the data.
In general, any plot will respect the data given, so for smoother visuals you will need to process the data itself somehow, in this case you need either finer resolution (each datapoint appears bigger), or less detail (fewer points).
You can downscale your data somehow (ideally not just sub-sampling, but that does work !), alternatively some local averaging like
b = 0.25 * (a[::2, ::2] + a[::2, 1::2] + a[1::2, ::2] + a[1::2, 1::2])
(I know, a bit nasty and doesn't scale !) For nice visuals results, you can leave it at the existing resolution (or even upscale) but explicitly smooth it, e.g. with a rolling window average.