Try drawing a map with python + cartopy 0.18.0

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.

usage environment

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.)

Library import

#In[1]
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np

Equirectangular projection

Basic

#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]

Out2.png

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.

Change the position of the grid

You can change the position of the grid by passing an iterable object with a grid or Locator to xlocs, ylocs of ʻax.gridlines ()`.

Use 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]

Out3.png

Apparently, the longitude range must be specified in the range of -180 to 180.

Use Multiple Locator

#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]

Out4.png

If you pass dms = True to ʻax.gridlines ()`, you can display the axis labels in degrees (degree), minutes (minute), and seconds (second) notation.

Erase the axis label on the map

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]

Out5.png

Stereographic projection

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] Out6.png

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.

Summary

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

Try drawing a map with python + cartopy 0.18.0
Try drawing a map with Python's folium package
Try drawing a simple animation in Python
[Python] Drawing a swirl pattern with turtle
Try HTML scraping with a Python library
I made a Hex map with Python
Try scraping with Python.
Drawing with Python Tinker
Try to draw a life curve with python
Try to make a "cryptanalysis" cipher with Python
Try to make a dihedral group with Python
Visualize grib2 on a map with python (matplotlib)
[Python, ObsPy] I drew a beach ball on the map with Cartopy + ObsPy.
Try to make a command standby tool with python
Try programming with a shell!
Try Python output with Haxe 3.2
I tried to draw a route map with Python
Make a fortune with Python
Try running Python with Try Jupyter
Try face recognition with Python
Create a directory with python
What is a python map?
Try running python in a Django environment created with pipenv
[Python] Try optimizing FX systole parameters with a genetic algorithm
Try to bring up a subwindow with PyQt5 and Python
A sample for drawing points with PIL (Python Imaging Library).
[Python] What is a with statement?
Try scraping with Python + Beautiful Soup
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Let's make a GUI with python.
Try to operate Facebook with Python
Drawing a silverstone curve using python
Solve ABC166 A ~ D with Python
Try singular value decomposition with Python
Building a virtual environment with Python 3
Solve ABC168 A ~ C with Python
Make a recommender system with python
Try face recognition with python + OpenCV
[Python] Generate a password with Slackbot
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Try frequency control simulation with Python
[Python] Inherit a class with class variables
I made a daemon with Python
Easily draw a map with matplotlib.basemap
Write a batch script with Python3.5 ~
Try python
Try to create a python environment with Visual Studio Code & WSL
Try to extract a character string from an image with Python3
Try adding a wall to your IFC file with IfcOpenShell python
[Pyenv] Building a python environment with ubuntu 16.04
[Python] Calendar-style heat map (with holiday display)
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Try to reproduce color film with Python
Create a Python function decorator with Class
Creating a simple PowerPoint file with Python
[Python] A program that creates stairs with #
Try logging in to qiita with Python
Building a Python3 environment with Amazon Linux2