Cartopy does not draw coastlines when I change projection

30 views Asked by At

I am trying to plot the coastlines in the north part of the Mediterranean Sea.

The following script, that uses PlateCarre projection, works:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from sys import exit as sys_exit


def main():
    plt.figure(dpi=600)
    ax = plt.axes(projection=ccrs.PlateCarree())

    lon1, lon2, lat1, lat2 = 12., 16., 43.3, 46.
    ax.set_extent([lon1, lon2, lat1, lat2], crs=ccrs.PlateCarree())
    ax.coastlines()

    plt.savefig('test.pdf')
    plt.close()

    return 0


if __name__ == '__main__':
    sys_exit(main())

If I replace ccrs.PlateCarree with ccrs.Mercator, instead, I get an empty image. Here is the script with the replaced projection:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from sys import exit as sys_exit


def main():
    plt.figure(dpi=600)
    ax = plt.axes(projection=ccrs.Mercator())

    lon1, lon2, lat1, lat2 = 12., 16., 43.3, 46.
    ax.set_extent([lon1, lon2, lat1, lat2], crs=ccrs.Mercator())
    ax.coastlines()

    plt.savefig('test.pdf')
    plt.close()

    return 0


if __name__ == '__main__':
    sys_exit(main())

Can you tell me what I am doing wrong?

1

There are 1 answers

2
RuthC On BEST ANSWER

The Mercator coordinate system does not use degrees. If you still want to specify the extent in degrees you can use the PlateCarree crs for that.

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

plt.figure()
ax = plt.axes(projection=ccrs.Mercator())

lon1, lon2, lat1, lat2 = 12., 16., 43.3, 46.
ax.set_extent([lon1, lon2, lat1, lat2], crs=ccrs.PlateCarree())
ax.coastlines()

plt.show()

enter image description here