Skip to content

Commit d276da0

Browse files
committed
Fix #1530
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
1 parent 32b203b commit d276da0

6 files changed

Lines changed: 741 additions & 259 deletions

File tree

docs/source/contributors.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ the package wouldn't be as rich or diverse as it is today:
4040
* Greg Lucas
4141
* Sadie Bartholomew
4242
* Kacper Makuch
43+
* Stephane Raynaud
44+
4345

4446
Thank you!
4547

lib/cartopy/mpl/geoaxes.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,9 @@ def gridlines(self, crs=None, draw_labels=False,
13741374
xlocs=None, ylocs=None, dms=False,
13751375
x_inline=None, y_inline=None, auto_inline=True,
13761376
xformatter=None, yformatter=None, xlim=None, ylim=None,
1377-
**kwargs):
1377+
rotate_labels=None, xlabel_style=None, ylabel_style=None,
1378+
labels_bbox_style=None, xpadding=5, ypadding=5,
1379+
offset_angle=25, auto_update=False, **kwargs):
13781380
"""
13791381
Automatically add gridlines to the axes, in the given coordinate
13801382
system, at draw time.
@@ -1386,7 +1388,25 @@ def gridlines(self, crs=None, draw_labels=False,
13861388
which gridlines are drawn.
13871389
Defaults to :class:`cartopy.crs.PlateCarree`.
13881390
draw_labels: optional
1389-
Label gridlines like axis ticks, around the edge.
1391+
Toggle whether to draw labels. For finer control, attributes of
1392+
:class:`Gridliner` may be modified individually. Defaults to False.
1393+
1394+
- string: "x" or "y" to only draw labels of the respective
1395+
coordinate in the CRS.
1396+
- list: Can contain the side identifiers and/or coordinate
1397+
types to select which ones to draw.
1398+
For all labels one would use
1399+
`["x", "y", "top", "bottom", "left", "right", "geo"]`.
1400+
- dict: The keys are the side identifiers
1401+
("top", "bottom", "left", "right") and the values are the
1402+
coordinates ("x", "y"); this way you can precisely
1403+
decide what kind of label to draw and where.
1404+
For x labels on the bottom and y labels on the right you
1405+
could pass in `{"bottom": "x", "left": "y"}`.
1406+
1407+
Note that, by default, x and y labels are not drawn on left/right
1408+
and top/bottom edges respectively unless explicitly requested.
1409+
13901410
xlocs: optional
13911411
An iterable of gridline locations or a
13921412
:class:`matplotlib.ticker.Locator` instance which will be
@@ -1433,6 +1453,36 @@ def gridlines(self, crs=None, draw_labels=False,
14331453
way to the edge of the boundary. ylim can be a single number or
14341454
a (min, max) tuple. If a single number, the limits will be
14351455
(-ylim, +ylim).
1456+
rotate_labels: optional, bool, str
1457+
Allow the rotation of non-inline labels.
1458+
1459+
- False: Do not rotate the labels.
1460+
- True: Rotate the labels parallel to the gridlines.
1461+
- None: no rotation except for some projections (default).
1462+
- A float: Rotate labels by this value in degrees.
1463+
1464+
xlabel_style: dict
1465+
A dictionary passed through to ``ax.text`` on x label creation
1466+
for styling of the text labels.
1467+
ylabel_style: dict
1468+
A dictionary passed through to ``ax.text`` on y label creation
1469+
for styling of the text labels.
1470+
labels_bbox_style: dict
1471+
bbox style for all text labels.
1472+
xpadding: float
1473+
Positive of negative padding for x labels.
1474+
When negative, the labels is draw inside the map.
1475+
ypadding: float
1476+
Positive of negative padding for y labels.
1477+
When negative, the labels is draw inside the map.
1478+
offset_angle: float
1479+
Difference of angle in degrees from 90 to define when
1480+
a label must be flipped to be more readable.
1481+
For example, a value of 10 makes a vertical top label to be
1482+
flipped only at 100 degrees.
1483+
auto_update: bool
1484+
Whether to update the grilines and labels when the plot is
1485+
refreshed.
14361486
14371487
Keyword Parameters
14381488
------------------
@@ -1461,7 +1511,12 @@ def gridlines(self, crs=None, draw_labels=False,
14611511
self, crs=crs, draw_labels=draw_labels, xlocator=xlocs,
14621512
ylocator=ylocs, collection_kwargs=kwargs, dms=dms,
14631513
x_inline=x_inline, y_inline=y_inline, auto_inline=auto_inline,
1464-
xformatter=xformatter, yformatter=yformatter, xlim=xlim, ylim=ylim)
1514+
xformatter=xformatter, yformatter=yformatter,
1515+
xlim=xlim, ylim=ylim, rotate_labels=rotate_labels,
1516+
xlabel_style=xlabel_style, ylabel_style=ylabel_style,
1517+
labels_bbox_style=labels_bbox_style,
1518+
xpadding=xpadding, ypadding=ypadding, offset_angle=offset_angle,
1519+
auto_update=auto_update)
14651520
self._gridliners.append(gl)
14661521
return gl
14671522

0 commit comments

Comments
 (0)