Skip to content

Commit

Permalink
ticks can now be placed on either side of plot
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsofbits committed Mar 27, 2020
1 parent f4d50ec commit 0146150
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 16 deletions.
54 changes: 50 additions & 4 deletions doc/Examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,10 +699,11 @@ def add_gridlabels(fig, ax, gl, lons=None, lats=None, **kwargs):
df = df.sort_values(by='timestamp')
n =120
reload()
with pyseas.context(pyseas.styles.light):
fig = plt.figure(figsize=(9, 12), frameon=True)
ax = maps.create_map(projection='regional.south_pacific')
gl = maps.add_gridlines(ax)
with pyseas.context(pyseas.styles.dark):
fig = plt.figure(figsize=(9, 9), frameon=True)
ax = maps.create_map(projection='regional.european_union')
gl = maps.add_gridlines(ax, color='white', alpha=0.7)
maps.add_raster(ax, img[::40, ::40])
maps.add_gridlabels(ax, gl)
maps.add_land(ax)
maps.add_countries(ax)
Expand All @@ -711,4 +712,49 @@ def add_gridlabels(fig, ax, gl, lons=None, lats=None, **kwargs):
plt.show()


# +
reload()

ssvids = sorted(set(fishing_df.ssvid))[1:]

# TODO: figure out style for dark panel (use gfw.panel.background as color)
# See Juan Carlos's example in this post:
# https://globalfishingwatch.slack.com/archives/CUW93UNLS/p1585062098015100
# (Obvious things: background, and line/tick colors)
with pyseas.context(pyseas.styles.light):
with pyseas.context({'gfw.fig.background' : 'white',
'gfw.ocean.color' : 'white',
'gfw.fig.background' : 'white'}):
for ssvid in ssvids:

dfn = fishing_df[fishing_df.ssvid == ssvid]
dfn = dfn.sort_values(by='timestamp')
is_fishing = (dfn.nnet_score > 0.5)

fig = plt.figure(figsize=(12, 12))
info = plot_tracks.plot_fishing_panel(dfn.timestamp, dfn.lon,
dfn.lat, is_fishing,
plots = [
# {'label' : 'lon', 'values' : dfn.lon},
# {'label' : 'lat', 'values' : dfn.lat},
{'label' : 'speed (knots)', 'values' : medfilt(dfn.speed.values,11),
'min_y' : 0},
{'label' : 'depth (km)', 'values' : -dfn.elevation_m / 1000,
'min_y' : 0, 'invert_yaxis' : True},
],
map_ratio=6,
annotations=7,
annotation_y_shift=0.5)

# maps.add_scalebar(info.map_ax, info.extent)
gl = maps.add_gridlines(info.map_ax)
maps.add_gridlabels(info.map_ax, gl, lat_side='right', lon_side='top')
maps.add_figure_background(fig)

plt.savefig('/Users/timothyhochberg/Desktop/test_fpanel.png', dpi=300,
facecolor=plt.rcParams['gfw.fig.background'])

plt.show()
# -


7 changes: 4 additions & 3 deletions pyseas/maps/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ def add_gridlines(ax, zorder=0.5, **kwargs):
kwargs[name] = plt.rcParams['grid.' + name]
return ax.gridlines(zorder=zorder, **kwargs)

def add_gridlabels(ax, gl, lons=None, lats=None, fig=None, **kwargs):
def add_gridlabels(ax, gl, lons=None, lats=None, fig=None,
lon_side='bottom', lat_side='left', **kwargs):
if fig is None:
fig = plt.gcf()
extent = ax.get_extent(crs=identity)
Expand All @@ -368,8 +369,8 @@ def add_gridlabels(ax, gl, lons=None, lats=None, fig=None, **kwargs):
fig.canvas.draw()
ax.xaxis.set_major_formatter(ticks.LONGITUDE_FORMATTER)
ax.yaxis.set_major_formatter(ticks.LATITUDE_FORMATTER)
ticks.lambert_xticks(ax, lons)
ticks.lambert_yticks(ax, lats)
ticks.draw_xticks(ax, lons, side=lon_side)
ticks.draw_yticks(ax, lats, side=lat_side)

def create_map(subplot=(1, 1, 1),
projection='global.default',
Expand Down
26 changes: 17 additions & 9 deletions pyseas/maps/ticks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,33 @@ def find_side(ls, side):

EPS = 1 # TODO: Added padding, clean up and comment

def lambert_xticks(ax, ticks):
"""Draw ticks on the bottom x-axis of a Lambert Conformal projection."""
def draw_xticks(ax, ticks, side='bottom'):
"""Draw ticks on the bottom x-axis of a cartopy map."""
assert side in ['bottom', 'top']
te = lambda xy: xy[0]
lc = lambda t, n, b: np.vstack((np.zeros(n) + t, np.linspace(b[2] - EPS, b[3] + EPS, n))).T
xticks, xticklabels = _lambert_ticks(ax, ticks, 'bottom', lc, te)
ax.xaxis.tick_bottom()
xticks, xticklabels = _ticks(ax, ticks, side, lc, te)
if side == 'bottom':
ax.xaxis.tick_bottom()
else:
ax.xaxis.tick_top()
ax.set_xticks(xticks)
ax.set_xticklabels([ax.xaxis.get_major_formatter()(xtick) for xtick in xticklabels])

def lambert_yticks(ax, ticks):
"""Draw ricks on the left y-axis of a Lamber Conformal projection."""
def draw_yticks(ax, ticks, side='left'):
"""Draw ticks on the left y-axis of a Lamber Conformal projection."""
assert side in ['left', 'right']
te = lambda xy: xy[1]
lc = lambda t, n, b: np.vstack((np.linspace(b[0] - EPS, b[1] + EPS, n), np.zeros(n) + t)).T
yticks, yticklabels = _lambert_ticks(ax, ticks, 'left', lc, te)
ax.yaxis.tick_left()
yticks, yticklabels = _ticks(ax, ticks, side, lc, te)
if side == 'left':
ax.yaxis.tick_left()
else:
ax.yaxis.tick_right()
ax.set_yticks(yticks)
ax.set_yticklabels([ax.yaxis.get_major_formatter()(ytick) for ytick in yticklabels])

def _lambert_ticks(ax, ticks, tick_location, line_constructor, tick_extractor):
def _ticks(ax, ticks, tick_location, line_constructor, tick_extractor):
"""Get the tick locations and labels for an axis of a Lambert Conformal projection."""
outline_patch = sgeom.LineString(ax.outline_patch.get_path().vertices.tolist())
axis = find_side(outline_patch, tick_location)
Expand Down

0 comments on commit 0146150

Please sign in to comment.