Skip to content

Commit

Permalink
change the bin countour function so it uses a PatchCollection instead…
Browse files Browse the repository at this point in the history
… of plotting individual bins on the axis in a for loop
  • Loading branch information
philippemiron committed Oct 29, 2021
1 parent 4420b3c commit 7e56d58
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
26 changes: 14 additions & 12 deletions pygtm/physical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import numpy as np
import cartopy.crs as ccrs
from cartopy.mpl.geoaxes import GeoAxes

from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection

class physical_space:
def __init__(self, lon, lat, resolution):
Expand Down Expand Up @@ -140,31 +141,32 @@ def vector_to_matrix(self, vector):
mat[self.id_og] = vector
return np.ma.masked_invalid(mat.reshape((self.ny - 1, self.nx - 1)))

def bins_contour(self, ax, color='k', bin_id=None):
def bins_contour(self, ax, edgecolor='k', bin_id=None, projection=None):
"""
Plot all element bins on one axis
Args:
ax: axis to plot on
color: bins contour color
edgecolor: bins contour color
bin_id: which bins to plot (default all)
projection: add transform keyword to convert to cartopy projection
"""
if bin_id is None:
bins = self.bins
else:
bins = self.bins[bin_id]

patches = []
for b_i in bins:
# corners and width/height of element
c = (self.coords[b_i[0]][0], self.coords[b_i[0]][1])
w = self.coords[b_i[1]][0] - self.coords[b_i[0]][0]
h = self.coords[b_i[2]][1] - self.coords[b_i[0]][1]
patches.append(Rectangle(c, w, h, edgecolor=edgecolor, fill=False, linewidth=0.5))

if projection is not None:
p = PatchCollection(patches, transform=projection, match_original=True)
else:
p = PatchCollection(patches, match_original=True)

if isinstance(ax, GeoAxes):
ax.plot([c[0], c[0] + w, c[0] + w, c[0], c[0]],
[c[1], c[1], c[1] + h, c[1] + h, c[1]],
color, linewidth=0.2, zorder=1, transform=ccrs.PlateCarree())
else:
ax.plot([c[0], c[0] + w, c[0] + w, c[0], c[0]],
[c[1], c[1], c[1] + h, c[1] + h, c[1]],
color, linewidth=0.2, zorder=1)
return
ax.add_collection(p)
return
7 changes: 4 additions & 3 deletions tutorials/01-physical_domain.ipynb

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions tutorials/02-search_elements.ipynb

Large diffs are not rendered by default.

1 comment on commit 7e56d58

@philippemiron
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix #5

Please sign in to comment.