[PYTHON] Avoiding the case where the contour goes around the earth in the opposite direction when the contour label is written with Cartopy

environment

Python 3.7.4 Matplotlib 3.1.1 Cartopy 0.17.0

What happened?

I don't know how to express it, so I will first introduce the diagram of the problem. The data is a certain meteorological data (JRA-55, 200hPa Geopotential Height [gpm]). t-1.png The contours (isolines) are not drawn normally, and are they all in the opposite direction near 0 degree longitude? Will rotate at the same latitude. As shown in the figure, we recognize that it is a problem that occurs in the projection drawing including poles (here, ccrs.AzimuthalEquidistant ()).

According to a Stackoverflow article (https://stackoverflow.com/questions/43238645/strange-behavior-with-contours-in-cartopy-polar-stereographic-plot?rq=1), don't forget the arrangement of longitudes. There is a request to make it cyclic from east to west, but it was originally done. (Add_cyclic_point is also explained visually in this article.)

python


import numpy as np
from cartopy.util import add_cyclic_point

#Various omissions

lon = np.arange(0, 360, 1.25) #Here 358.Up to 75
f_cycle, lons = add_cyclic_point(f, cood=lon) #Expanded to 360, longitude information stored in lons, f is the data to draw

Conversely, if you draw without add_cyclic_point, t-0.png The problem of "reverse round" has been avoided, but there is a gap in the contour near 0 degrees longitude.

If you don't write a label, this problem will not occur in the first place. t-2.png

I've done my own research, but I haven't found a way around this problem right now. The 2018 Stackoverflow article (https://stackoverflow.com/questions/49048411/cartopy-north-pole-stereographic-contour-plot-does-not-plot-correctly-even-with) addresses this issue with Cartopy. It seems that the developers of the company are also cooperating, but what happened after that? .. (May be resolved in the latest version)

Compromise

The idea was to limit the contours to be labeled and not add_cyclic_point the contours. You can attach the minimum required label, but the contour with the label will still be cut off near 0 degrees longitude.

#Sequence for labels
lev1 = np.arrange(0, 200001, 100) #Narrow spacing
lev1n = lev1 #Duplicate for when leaving lev1
lev5 = np.arrange(0, 200001, 400) #Wide interval
for l in lev5:
    lev1n = lev1n[~(lev1n==l)] #lev1(lev1n)Remove the same elements as lev5 from

'''If you enumerate the contents
lev1:  0, 100, 200, 300, 400, 500, 600, ...
lev5:  0,                400,           ...
lev1n:    100, 200, 300,      500, 600, ...
'''

#Contour using lev1n, add_cyclic_With point, without label
c1 = ax.contour(lons, lat, f_cycle, lev1n, linewidth=0.8, transform=ccrs.PlateCarree())
#Contour using lev5, add_cyclic_No point, with label
c5 = ax.contour(lon, lat, f, lev5, linewidth=1.3, transform=ccrs.PlateCarree())
c5.clabel(fmt='%5.0f', fontsize=9)

It will be like this. It's just a compromise. .. t-3.png I referred to someone's article for the list comprehension notation of lev1n, but I will add it when I remember. I think there are other ways to do this, but if you don't add this process anyway, the narrower contour will be drawn on the back of the label, making it difficult to read.

I'm glad if you can use it as a reference.

Recommended Posts

Avoiding the case where the contour goes around the earth in the opposite direction when the contour label is written with Cartopy