matplotlib-label-lines
matplotlib-label-lines copied to clipboard
Request: allow plotting a labelLine outside the data range
To use the first example as a starting point, say I want to put all the labels for the lines to the right of the lines at x=1.05. Right now if I try to do the below, I get the error: ValueError: x label location is outside data range!
But I think it would be useful functionality to be able to place a label at an arbitrary x value for the closest data edge's y value.
An alternative way to get this behavior would be to allow passing in an xoffset for the labels.
import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import chi2, loglaplace
from labellines import labelLine
X = np.linspace(0, 1, 500)
A = [1, 2, 5, 10, 20]
funcs = [np.arctan, np.sin, loglaplace(4).pdf, chi2(5).pdf]
fig, ax = plt.subplots()
for a in A:
ax.plot(X, np.arctan(a * X), label=str(a))
for i in range(len(A)):
labelLine(ax.get_lines()[i], 1.05, zorder=2.5)
The difficulty with the general case is that you'd have to interpolate somehow the data outside of its range - and there are no unique choices for the interpolation: linear? same value? cubic? Your suggestion of having an x-offset seems easier.
Unfortunately, I don't have the bandwidth to implement any new feature but I would be happy to review any PR if you want to have a go!