Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better label rotation #1475

Closed

Conversation

stefraynaud
Copy link
Contributor

Rationale

Labels are rectangles with text and their rotation make them
to overlap the map boundary, which force the current algorithm
to try another rotation angle, and eventually leads to suprising
rotations. This occurs because the labels are also moved along
an ellipse that is defined xpadding and ypadding.

This patch make the labels to move along the rectangle defined by xpadding
and yappding, the one that encloses the ellipse. This
sligthly increases the padding which may be sufficient to prevent
labels to overlap boundaries in most situations.

Implications

Labels should undergo less exagerated rotation in some circumstances.

Labels are rectangles with text and their rotation make them
to overlap the map boundary, which force the current algorithm
to try another rotation angle, and eventually leads to suprising
rotations. This occurs because the labels are also moved along
an ellipse that is defined xpadding and ypadding.

This patch make the labels to move along the rectangle defined by xpadding
and yappding, the one that encloses the ellipse. This
sligthly increases the padding which may be sufficient to prevent
labels to overlap boundaries in most situations.
@QuLogic
Copy link
Member

QuLogic commented Feb 27, 2020

Can you post an example of what effect this might have?

@stefraynaud
Copy link
Contributor Author

Here is a view extracted from a figure that is created by unitary tests.
cartopy-rotation

cartopy-rotation2

On the left, that actual behavior, and on the right, the proposed one. Globally: some of the labels are less rotated, and some others appear.

@greglucas
Copy link
Contributor

I actually think the addition makes the axes formatting look worse. I personally liked the rotations before... Maybe this is because of the extra locations though, so perhaps it just needs an updated locator now?

@stefraynaud
Copy link
Contributor Author

Here is one of the reason I proposed this PR: #1560

In fact, when can even go further by not trying alternate angles that induce confusion, but play only with the padding, and just not display labels that intersect the boundary with gridline angle.

@friedrichknuth
Copy link

For this example

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

rotated_crs = ccrs.RotatedPole(pole_longitude=120.0, pole_latitude=70.0)

ax = plt.axes(projection=rotated_crs)
ax.set_extent((-6, 3, 48, 58), crs=ccrs.PlateCarree())
ax.coastlines(resolution='50m')

gl = ax.gridlines(draw_labels=True,x_inline=False, y_inline=False)
plt.show()

the proposed fix in this PR produces

PR

instead of

current

The thought with #1560 is something closer to

suggested

but also with labels at the top and bottom of the plot, oriented similarly to the way labels seen at the right and left are being aligned in parallel to gridlines.

@stefraynaud
Copy link
Contributor Author

@friedrichknuth Aligning top and bottom labels with the gridline without 90° rotation to make it more readble is choice among others. For instance with this choice, longitude labels will not longer be horizontal in classic rectangular projections (like this figure).
I think, a solution that may work in most situation is:

  • to always align the strictly with the gridlines,
  • make by default text label as horizontal as possible (which is already done)
  • increase the padding until if the labels crosses the map boundary, until a max padding
  • if the max padding is reached, try a 90° rotation of the label that may make it vertical.
  • finally don't show the label if it still crosses the map boundary.

It's for debate.

Note:

  • problems occur when the label size is large with respect to the padding
  • the solution must work with all projections and map boundary types

@friedrichknuth
Copy link

friedrichknuth commented May 15, 2020

The issue is indeed tricky... in the example provided above

gl.top_labels = False
gl.right_labels = False

produces

plot

which helps clean things up. What remains are unexpected labels on the right side, despite the call for all labels on the right to be disabled.

I think this is being discussed in part here #1530. There might be a lead in #1530 (comment) on how to mask right/left/top/bottom values based on if they are lat/lon values. In other words, if labels on a given side are being explicitly disabled, only preserve the most frequent type.

Perhaps only displaying the most frequent type (lat or lon) on a given side can be the default? Not sure how this plays out in all possible projections/rotations, but it may resolve some of these alignment issues.

Another thought would be to implement a toggle for gl.rotate_labels by axes (e.g. gl.rotate_xlabels and gl.rotate_ylabels. False would mean horizontal and True could mean for the text to be aligned parallel to the gridline angle or orthogonally, in the case of top/bottom labels.

@stefraynaud
Copy link
Contributor Author

In fact, the problem of label visibility is also tricky. For instance, what is a "right label" in this figure?
Whatever, it should be better with the PR #1537 and #1538 that are currently under review.

As for the rotate_labels parameter, currently True means "we try to align it with the gridline", and False means "horizontal". As discussed above, this would be changed to "we strictly align it or hide it". It is also feasible as you suggest to allow a distinction between "x" labels rotation and "y" labels rotation.

@stefraynaud
Copy link
Contributor Author

No longer valid due to #1537

@stefraynaud stefraynaud closed this May 4, 2021
@stefraynaud stefraynaud deleted the better_label_rotation branch September 15, 2021 06:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants