When drawing a map with cartopy 0.18.0 or earlier, there were problems such as the axis label supporting only equirectangular projection and UTM projection, and the format of the axis label being suspicious. I wrote this article about adjusting the axis label before, but this time the adjustment function around the axis of cartopy has been considerably enhanced, so the axis is very concise. You can now write labels.
In this article, we will use the latest features of cartopy to plot a map with adjusted axis labels.
python=3.7 numpy=1.18.1 cartopy=0.18.0 matplotlib=3.2.1 jupyterlab=2.1.1 I have launched and running a virtual environment in Anaconda.
(As an aside, I used to use Jupyter Notebook, but now I'm aware of the usefulness of Jupyterlab, so I moved.)
#In[1]
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np
#In[2]
fig=plt.figure(figsize=(10,5),facecolor='w')
ax=fig.add_subplot(1,1,1,projection=ccrs.PlateCarree(central_longitude=180))
ax.coastlines()
ax.set_global()
ax.gridlines(draw_labels=True)
Out[2]
You can draw a grid on the map with ʻax.gridlines () . At this time, the axis labels can also be displayed by setting
draw_labels = True`.
In the previous cartopy, there was a bug that EW overlapped with the 180 ° notation, but it seems that it has been fixed.
You can change the position of the grid by passing an iterable object with a grid or Locator
to xlocs
, ylocs
of ʻax.gridlines ()`.
np.arange ()
#In[3]
fig=plt.figure(figsize=(10,5),facecolor='w')
ax=fig.add_subplot(1,1,1,projection=ccrs.PlateCarree(central_longitude=180))
ax.coastlines()
ax.set_global()
ax.gridlines(draw_labels=True,xlocs=np.arange(-180,180.1,60)
,ylocs=np.arange(-90.,90.1,30))
Out[3]
Apparently, the longitude range must be specified in the range of -180 to 180.
#In[4]
mloc=plt.MultipleLocator(2.5) #Grid spacing 2.Generate Multiple Locator at 5 °
fig=plt.figure(figsize=(10,5),facecolor='w')
ax=fig.add_subplot(1,1,1,projection=ccrs.PlateCarree(central_longitude=180))
ax.coastlines(resolution='10m')
ax.gridlines(draw_labels=True,xlocs=mloc,ylocs=mloc,dms=True)
ax.set_extent([125,145,30,45]) #Display only in Japan
Out[4]
If you pass dms = True
to ʻax.gridlines ()`, you can display the axis labels in degrees (degree), minutes (minute), and seconds (second) notation.
If you want to remove the axis label on the map, get the cartopy.mpl.gridliner.Gridliner
instance from the return value of ʻax.gridlines ()` and rewrite the property as follows.
#In[5]
fig=plt.figure(figsize=(10,5),facecolor='w')
ax=fig.add_subplot(1,1,1,projection=ccrs.PlateCarree(central_longitude=180))
ax.coastlines()
ax.set_global()
gl=ax.gridlines(draw_labels=True)
gl.top_labels=False
Out[5]
As mentioned above, it is now possible to attach axis labels in addition to equirectangular projection and Mercator projection.
Here, let's draw a stereographic projection centered on Antarctica.
#In[6]
fig=plt.figure(figsize=(8,8),facecolor='w')
ax=fig.add_subplot(1,1,1,projection=ccrs.SouthPolarStereo(central_longitude=180))
ax.coastlines(resolution='50m')
gl=ax.gridlines(draw_labels=True,linestyle='--',xlocs=plt.MultipleLocator(20)
,ylocs=plt.MultipleLocator(15))
gl.xlabel_style={'size':18,'color':'red'}
gl.ylabel_style={'size':18,'color':'green'}
ax.set_extent([-180,180.1,-90,0],ccrs.PlateCarree())
OUT[6]
The size and color of the axis label can be adjusted by setting the values in the dictionary to gl.xlabel_style
and gl.ylabel_style
.
By using ʻax.gridlines () and the generated
Gridliner` instance with the cartopy update, it is no longer necessary to make fine adjustments to the axis labels on the matplotlib side. I feel that it has become very easy to use.
I would like to add more if I feel like plotting the contour diagram.
I would appreciate it if you could point out anything strange.
Recommended Posts