简体   繁体   English

如何使用matplotlib在Cartopy极地立体图中获得平滑的网格线?

[英]How do I get smooth gridlines in cartopy polar stereographic plots with matplotlib?

When plotting with matplotlib and cartopy's NorthPolarStereo projection, the constant-latitude gridlines (parallels) are not smooth circles. 使用matplotlib和cartopy的NorthPolarStereo投影进行绘图时,恒定纬度网格线(平行线)不是平滑圆。

Here is a minimum working example: 这是一个最小的工作示例:

from matplotlib import pyplot
import cartopy.crs

ax = pyplot.axes(projection=cartopy.crs.NorthPolarStereo())
ax.set_extent((-180, 180, 60, 90), crs=cartopy.crs.PlateCarree())
ax.gridlines()

pyplot.show()

在此输入图像描述

As you can see, the parallels are a series of straight-line segments between points. 如您所见,平行线是点之间的一系列直线段。 There aren't enough points along each line to make them appear smooth. 每条线上没有足够的点使它们看起来光滑。 How can I make the parallels more circular? 如何使平行线更圆?

Q: How can I make the parallels more circular? 问:如何使平行线更圆?

A: You need larger value of n_steps property of the gridliner object created by gridlines() . 答:您需要更大的gridlines()创建的gridliner对象的n_steps属性值。 Its default value is 30. Here is the relevant code that sets the value to 90, and generates better plot. 它的默认值是30.这是将值设置为90的相关代码,并生成更好的绘图。

gl = ax.gridlines(draw_labels=False, xlocs=None, ylocs=None)
gl.n_steps = 90

See reference here . 这里的参考。

Sample output: 样本输出:

在此输入图像描述

This is due to the default threshold being too large for this use case. 这是由于此用例的默认阈值太大。 This is not a user-controllable part of Cartopy's projection interface. 这不是Cartopy投影界面的用户可控部分。 You can hack around it using the private _threshold attribute, dividing it by 100 looks reasonable for your use case: 你可以使用private _threshold属性来破解它,将它除以100对你的用例来说是合理的:

from matplotlib import pyplot
import cartopy.crs

hacked_proj = cartopy.crs.NorthPolarStereo()
hacked_proj._threshold /= 100.
ax = pyplot.axes(projection=hacked_proj)
ax.set_extent((-180, 180, 60, 90), crs=cartopy.crs.PlateCarree())
ax.gridlines()

pyplot.show()

This is not a great solution because it uses the implementation details of the Stereographic class, it would be better to have better setting of this in Cartopy. 这不是一个很好的解决方案,因为它使用了Stereographic类的实现细节,最好在Cartopy中更好地设置它。 Opening a ticket on the Cartopy Github repo with this use case would be a good start https://github.com/SciTools/cartopy/issues . 使用此用例在Cartopy Github仓库上打开票证将是一个很好的开始https://github.com/SciTools/cartopy/issues

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

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