Last active
March 6, 2019 10:58
-
-
Save yaswant/d082b41d81d953491b83424363ed4628 to your computer and use it in GitHub Desktop.
strorm track with customised legend on cartopy map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Demo: Plot strom tracks with custom legend on Cartopy mapping system | |
""" | |
import numpy as np | |
import cartopy.crs as ccrs | |
import cartopy.feature as cfeature | |
from cartopy.mpl import gridliner | |
import matplotlib.pyplot as plt | |
import matplotlib.patches as mpatches | |
from collections import OrderedDict | |
# Create some dummy tracks (plane coordinates) and their properties | |
events = OrderedDict( | |
TD=np.array([[80,80,80,80],[5, 7, 9, 11]], dtype=np.float), | |
TC=np.array([[80,79,80,81, 88],[11, 11, 11, 15, 15]], dtype=np.float) | |
) | |
labels = ['TD', 'TC'] | |
colors = ['b', 'r'] | |
extent = [50,120, -10, 30] # x0, x1, y0, y1 | |
legend_kw = dict(loc=9, bbox_to_anchor=(0.5, -0.1), ncol=2, frameon=False) | |
''' | |
dummy tracks and map extent defined here are on a cartesian plane; | |
update the projection parameters for actual data source. | |
See Cartopy supported projection list at | |
https://scitools.org.uk/cartopy/docs/latest/crs/projections.html | |
''' | |
data_proj = ccrs.PlateCarree() | |
# Example 1: Regular Data on Rectangular (PlateCarre) projection | |
plot_proj = ccrs.PlateCarree() | |
ax = plt.axes(projection=plot_proj) | |
ax.set_extent(extent, data_proj) | |
ax.coastlines() | |
gl = ax.gridlines(draw_labels=True) | |
gl.xformatter = gridliner.LONGITUDE_FORMATTER | |
gl.yformatter = gridliner.LATITUDE_FORMATTER | |
gl.xlabels_top, gl.ylabels_right = False, False | |
ax.add_feature(cfeature.BORDERS, lw=0.4, edgecolor='k') | |
patch = [] | |
for i, item in enumerate(events): | |
ax.plot(events[item][0], events[item][1], c=colors[i], transform=data_proj) | |
patch.append(mpatches.Patch(color=colors[i], label=labels[i])) | |
_ = ax.legend(handles=patch, **legend_kw) | |
# Example 2: Regular Data on Robinson projection | |
# Limitation: Cartopy doesnot offer grid labeling in the projection | |
plot_proj = ccrs.Robinson() | |
ax = plt.axes(projection=plot_proj) | |
ax.set_extent(extent, data_proj) | |
ax.coastlines() | |
gl = ax.gridlines() | |
ax.add_feature(cfeature.BORDERS, lw=0.4, edgecolor='k') | |
patch = [] | |
for i, item in enumerate(events): | |
ax.plot(events[item][0], events[item][1], c=colors[i], transform=data_proj) | |
patch.append(mpatches.Patch(color=colors[i], label=labels[i])) | |
_ = ax.legend(handles=patch, **legend_kw) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment