Skip to content

Commit

Permalink
Fix #1530
Browse files Browse the repository at this point in the history
It now takes into account the side label visibility in the rotation of labels.

Add better and more control to gridliner labels drawing

* draw_labels accepts more than a boolean.
* label positioning is better estimates thanks to side spines

Clean gridliner

Improve the rotation and visibility of gridline labels

- the location of labels takes spines into account
- lines and labels are redrawn when zooming
- the rotation of labels is both more tunable and strict
- added the inside parameter for drawing labels inside the the boundary

Fix stickler

Fix padding

Fix the rotation of labels

Possible values: True, False, None

None: it depends on the projection.
True: rotate along gridlines.
False: horizontal.

Another type of location for labels in addition
to left, right, top and bottom: geo.

By the way, add more control to the gridliner init by adding
the xpadding and ypadding keywords.

Add support for float values of rotate_labels

Also expose xlabel_style, ylabel_style and labels_bbox_style as
keyword argument to gridlines() and Gridliner().
Note that labels_bbox_style no longer override the bbox value
if x/ylabel_style.

Fix alignments

Fix labels in classic mode

Fix gridliner labels

Add Label class

Fix _get_loc_from_spine_intersection

Fix case where gridline stop before map boundary

Update of the baseline images

Fix linter

Fix gridliner

Fix gridliner compat with old mpl

Fix artist.update call

Fix labels drawing

Add more help for draw_labels

fix linting

Add myself to contributors

Fix docstrings

Fix linting
  • Loading branch information
stefraynaud committed May 10, 2021
1 parent 05436f6 commit 3f9c365
Show file tree
Hide file tree
Showing 6 changed files with 741 additions and 259 deletions.
2 changes: 2 additions & 0 deletions docs/source/contributors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ the package wouldn't be as rich or diverse as it is today:
* Greg Lucas
* Sadie Bartholomew
* Kacper Makuch
* Stephane Raynaud


Thank you!

Expand Down
61 changes: 58 additions & 3 deletions lib/cartopy/mpl/geoaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,9 @@ def gridlines(self, crs=None, draw_labels=False,
xlocs=None, ylocs=None, dms=False,
x_inline=None, y_inline=None, auto_inline=True,
xformatter=None, yformatter=None, xlim=None, ylim=None,
**kwargs):
rotate_labels=None, xlabel_style=None, ylabel_style=None,
labels_bbox_style=None, xpadding=5, ypadding=5,
offset_angle=25, auto_update=False, **kwargs):
"""
Automatically add gridlines to the axes, in the given coordinate
system, at draw time.
Expand All @@ -1386,7 +1388,25 @@ def gridlines(self, crs=None, draw_labels=False,
which gridlines are drawn.
Defaults to :class:`cartopy.crs.PlateCarree`.
draw_labels: optional
Label gridlines like axis ticks, around the edge.
Toggle whether to draw labels. For finer control, attributes of
:class:`Gridliner` may be modified individually. Defaults to False.
- string: "x" or "y" to only draw labels of the respective
coordinate in the CRS.
- list: Can contain the side identifiers and/or coordinate
types to select which ones to draw.
For all labels one would use
`["x", "y", "top", "bottom", "left", "right", "geo"]`.
- dict: The keys are the side identifiers
("top", "bottom", "left", "right") and the values are the
coordinates ("x", "y"); this way you can precisely
decide what kind of label to draw and where.
For x labels on the bottom and y labels on the right you
could pass in `{"bottom": "x", "left": "y"}`.
Note that, by default, x and y labels are not drawn on left/right
and top/bottom edges respectively unless explicitly requested.
xlocs: optional
An iterable of gridline locations or a
:class:`matplotlib.ticker.Locator` instance which will be
Expand Down Expand Up @@ -1433,6 +1453,36 @@ def gridlines(self, crs=None, draw_labels=False,
way to the edge of the boundary. ylim can be a single number or
a (min, max) tuple. If a single number, the limits will be
(-ylim, +ylim).
rotate_labels: optional, bool, str
Allow the rotation of non-inline labels.
- False: Do not rotate the labels.
- True: Rotate the labels parallel to the gridlines.
- None: no rotation except for some projections (default).
- A float: Rotate labels by this value in degrees.
xlabel_style: dict
A dictionary passed through to ``ax.text`` on x label creation
for styling of the text labels.
ylabel_style: dict
A dictionary passed through to ``ax.text`` on y label creation
for styling of the text labels.
labels_bbox_style: dict
bbox style for all text labels.
xpadding: float
Padding for x labels. If negative, the labels are
drawn inside the map.
ypadding: float
Padding for y labels. If negative, the labels are
drawn inside the map.
offset_angle: float
Difference of angle in degrees from 90 to define when
a label must be flipped to be more readable.
For example, a value of 10 makes a vertical top label to be
flipped only at 100 degrees.
auto_update: bool
Whether to update the grilines and labels when the plot is
refreshed.
Keyword Parameters
------------------
Expand Down Expand Up @@ -1461,7 +1511,12 @@ def gridlines(self, crs=None, draw_labels=False,
self, crs=crs, draw_labels=draw_labels, xlocator=xlocs,
ylocator=ylocs, collection_kwargs=kwargs, dms=dms,
x_inline=x_inline, y_inline=y_inline, auto_inline=auto_inline,
xformatter=xformatter, yformatter=yformatter, xlim=xlim, ylim=ylim)
xformatter=xformatter, yformatter=yformatter,
xlim=xlim, ylim=ylim, rotate_labels=rotate_labels,
xlabel_style=xlabel_style, ylabel_style=ylabel_style,
labels_bbox_style=labels_bbox_style,
xpadding=xpadding, ypadding=ypadding, offset_angle=offset_angle,
auto_update=auto_update)
self._gridliners.append(gl)
return gl

Expand Down
Loading

0 comments on commit 3f9c365

Please sign in to comment.