Fix label_line_ends behaviour when lines go past right xlim#44
Merged
Fix label_line_ends behaviour when lines go past right xlim#44
label_line_ends behaviour when lines go past right xlim#44Conversation
Contributor
|
@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:
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() |
Member
Author
|
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() |
eirikurj
approved these changes
May 14, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Purpose
The
label_line_endsfunction 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 changeslabel_line_endsto account for the x and y axis limitsPreviously:

With this fix:

Expected time until merged
Soon, but no rush
Type of change
Testing
Checklist
flake8andblackto make sure the Python code adheres to PEP-8 and is consistently formattedfprettifyor C/C++ code withclang-formatas applicable