Skip to content

Conversation

@A-CGray
Copy link
Member

@A-CGray A-CGray commented May 7, 2025

Purpose

The label_line_ends function puts the label at the point on the line with the largest x coordinate, but if this point is outside the axis limits then the label will be hidden. This PR changes label_line_ends to account for the x and y axis limits

Previously:
image

With this fix:
image

Expected time until merged

Soon, but no rush

Type of change

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (non-backwards-compatible fix or feature)
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no API changes)
  • Documentation update
  • Maintenance update
  • Other (please describe)

Testing

Checklist

  • I have run flake8 and black to make sure the Python code adheres to PEP-8 and is consistently formatted
  • I have formatted the Fortran code with fprettify or C/C++ code with clang-format as applicable
  • I have run unit and regression tests which pass locally with my changes
  • I have added new tests that prove my fix is effective or that my feature works
  • I have added necessary documentation

@A-CGray A-CGray requested a review from a team as a code owner May 7, 2025 16:18
@A-CGray A-CGray requested a review from eirikurj May 7, 2025 16:18
@eirikurj
Copy link
Contributor

eirikurj commented May 13, 2025

@A-CGray I tested this very briefly, but I am not sure if this is what you had in mind or not. This works well with lines with lots of data points, but does not have the best placement for coarse data (see snippet and figures below). Would it be possible to instead:

  • add the label after the line only if the line x is less than the axis x-range
  • add the label where the line intersects the axis if it goes beyond the axis range

Does this make sense? Maybe we want to consider this in a different PR?

Code

import matplotlib.pyplot as plt
import niceplots
fix, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 2, 3], label='Line 1')
ax.plot([1, 2, 3], [3, 1, 1], label='Line 2')
ax.plot([1, 2], [2, 3], label='Line 3')
ax.set_xlim(0.5, 2.8)
lines = ax.get_lines()
labels = [line.get_label() for line in lines]
niceplots.label_line_ends(ax, lines=lines, labels=labels)
plt.show()

Without
main
With this fix
fix

@A-CGray
Copy link
Member Author

A-CGray commented May 13, 2025

Good catch, fixed now

import matplotlib.pyplot as plt
import niceplots
import numpy as np

plt.style.use(niceplots.get_style())

fix, ax = plt.subplots()

# Test lines that go outside the axis limits on all 4 sides
for angle in np.linspace(0, 2 * np.pi, 6)[:-1]:
    x = 5.0 * np.cos(angle)
    y = 5.0 * np.sin(angle)
    ax.plot([0, x], [0, y], label=f"$\\theta$={np.rad2deg(angle):.0f} deg")
# Plot one line that is inside the axis limits
ax.plot([0, 0.5], [0, 0.5], label="Inside limits", color="red")
# And one that is totally outside the axis limits
ax.plot([200, 20], [200, 2], label="Totally outside limits", color="green")
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
niceplots.label_line_ends(ax)
niceplots.adjust_spines(ax)
plt.show()

image

@eirikurj eirikurj merged commit bf4625d into main May 14, 2025
17 checks passed
@A-CGray A-CGray deleted the lineEndLabelFix branch May 14, 2025 14:33
@A-CGray A-CGray mentioned this pull request May 14, 2025
13 tasks
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.

3 participants