diff --git a/lib/cartopy/crs.py b/lib/cartopy/crs.py index 90b254c66..ca1bf4065 100644 --- a/lib/cartopy/crs.py +++ b/lib/cartopy/crs.py @@ -156,6 +156,31 @@ def _determine_longitude_bounds(self, central_longitude): minlon += epsilon return minlon, maxlon + def _repr_html_(self): + import cgi + try: + # As matplotlib is not a core cartopy dependency, don't error + # if it's not available. + import matplotlib.pyplot as plt + except ImportError: + # We can't return an SVG of the CRS, so let Jupyter fall back to + # a default repr by returning None. + return None + + # Produce a visual repr of the Projection instance. + fig, ax = plt.subplots(figsize=(5, 3), + subplot_kw={'projection': self}) + ax.set_global() + ax.coastlines('auto') + ax.gridlines() + buf = six.StringIO() + fig.savefig(buf, format='svg', bbox_inches='tight') + plt.close(fig) + # "Rewind" the buffer to the start and return it as an svg string. + buf.seek(0) + svg = buf.read() + return '{}
{}
'.format(svg, cgi.escape(repr(self))) + def _as_mpl_axes(self): import cartopy.mpl.geoaxes as geoaxes return geoaxes.GeoAxes, {'map_projection': self}