Skip to content

Commit

Permalink
class labels_bbox_style variable have now default style value that ne…
Browse files Browse the repository at this point in the history
…eds to be overwritten
  • Loading branch information
eirik-mortensen committed Dec 18, 2020
1 parent 4c59ded commit f60e993
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
16 changes: 10 additions & 6 deletions lib/cartopy/mpl/gridliner.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ def __init__(self, axes, crs, draw_labels=False, xlocator=None,
#: for styling of the text labels.
self.ylabel_style = {}

# bbox style for grid labels
self.labels_bbox_style = {'pad': 0, 'visible': False}

#: The padding from the map edge to the x labels in points.
self.xpadding = 5

Expand Down Expand Up @@ -600,10 +603,11 @@ def _draw_gridliner(self, nx=None, ny=None, renderer=None):
for i, (pt0, pt1) in enumerate([tail, head]):
kw, angle, loc = self._segment_to_text_specs(
pt0, pt1, lonlat)
if not getattr(self, loc+'_labels'):
if not getattr(self, loc + '_labels'):
continue

kw.update(label_style,
bbox={'pad': 0, 'visible': False})
bbox=self.labels_bbox_style)
text = formatter(tick_value)

if self.y_inline and lonlat == 'lat':
Expand Down Expand Up @@ -646,7 +650,7 @@ def _segment_to_text_specs(self, pt0, pt1, lonlat):
"""Get appropriate kwargs for a label from lon or lat line segment"""
x0, y0 = pt0
x1, y1 = pt1
angle = np.arctan2(y0-y1, x0-x1) * 180 / np.pi
angle = np.arctan2(y0 - y1, x0 - x1) * 180 / np.pi
kw, loc = self._segment_angle_to_text_specs(angle, lonlat)
return kw, angle, loc

Expand Down Expand Up @@ -679,11 +683,11 @@ def _text_angle_to_specs_(self, angle, lonlat):

elif angle > 45:
loc = 'top'
kw.update(ha='center', va='bottom', rotation=angle-90)
kw.update(ha='center', va='bottom', rotation=angle - 90)

else:
loc = 'bottom'
kw.update(ha='center', va='top', rotation=angle+90)
kw.update(ha='center', va='top', rotation=angle + 90)

return kw, loc

Expand Down Expand Up @@ -760,7 +764,7 @@ def remove_path_dupes(path):
'va': artist.get_va()}
# Compute angles to try
angles = [None]
for abs_delta_angle in np.arange(delta_angle, max_delta_angle+1,
for abs_delta_angle in np.arange(delta_angle, max_delta_angle + 1,
delta_angle):
angles.append(artist._angle + abs_delta_angle)
angles.append(artist._angle - abs_delta_angle)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 26 additions & 3 deletions lib/cartopy/tests/mpl/test_gridliner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
LATITUDE_FORMATTER, LONGITUDE_FORMATTER,
classic_locator, classic_formatter)


from cartopy.tests.mpl import MPL_VERSION, ImageTesting


TEST_PROJS = [
ccrs.PlateCarree,
ccrs.AlbersEqualArea,
Expand Down Expand Up @@ -216,7 +214,6 @@ def test_grid_labels():
tolerance=grid_label_tol if ccrs.PROJ4_VERSION < (7, 1, 0)
else 4)
def test_grid_labels_tight():

# Ensure tight layout accounts for gridlines
fig = plt.figure(figsize=(7, 5))

Expand Down Expand Up @@ -307,9 +304,35 @@ def test_grid_labels_inline_usa():
else:
ax.gridlines(draw_labels=True, auto_inline=True, clip_on=True)
ax.coastlines(resolution="110m")

plt.subplots_adjust(wspace=0.35, hspace=0.35)


@ImageTesting(["gridliner_labels_bbox_style"], tolerance=grid_label_tol)
def test_gridliner_labels_bbox_style():
top = 49.3457868 # north lat
left = -124.7844079 # west long
right = -66.9513812 # east long
bottom = 24.7433195 # south lat

plt.figure(figsize=(6, 3))
ax = plt.subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.coastlines(resolution="110m")
ax.set_extent([left, right, bottom, top],
crs=ccrs.PlateCarree())
gl = ax.gridlines(draw_labels=True)

bbox_style = {
"pad": 0,
"visible": True,
"facecolor": "white",
"edgecolor": "black",
"boxstyle": "round, pad=0.2",
}

gl.labels_bbox_style = bbox_style


@pytest.mark.parametrize(
"proj,gcrs,xloc,xfmt,xloc_expected,xfmt_expected",
[
Expand Down

0 comments on commit f60e993

Please sign in to comment.