-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
abline() - for drawing arbitrary lines on a plot, given specifications. #5253
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
Comments
For those who aren't clicking through to the link, the proposal is mostly a Just yesterday, I was doing a correlation plot, and I was thinking to On Fri, Oct 16, 2015 at 4:14 AM, naught101 [email protected] wrote:
|
I'm definitely open to including this feature. Someone may need to step up to implement it, however. |
Cool. I might try and give it a go. Any tips on where to start would be helpful. In particular, I'm not sure how a (theoretically) infinite line would interact with how MPL defines the plot boundaries. Does the line just get re-drawn to the boundaries on every zoom/pan? |
I would look at how Axes.axhline() and Axes.axvline() are defined in On Mon, Oct 19, 2015 at 9:26 PM, naught101 [email protected] wrote:
|
On 2015/10/20 3:19 AM, Benjamin Root wrote:
I don't think this will work. axvline and axhline use blended |
Hrm.. I'm not sure what would be the most useful: to have an abline that works only in data coordinates (which presumably would draw spirals in polar coordinates, etc.), or one that works only in grid coordinates. I can't really see the latter being that useful outside of cartesian data coordinates anyway. But yeah, the blended transforms thing definitely won't work. I guess you can get the bounds of the axes, then transform those back into data coordinates, calculate your line segment by intersecting it with the rectangle, and then draw it in data coordinates? |
Start with (0,0)->(1,1) line and apply following transforms:
The hard part is getting the transpose of This also might not be the most efficient method... |
I think Elliot has the right idea. On Tue, Oct 20, 2015 at 10:58 PM, Elliott Sales de Andrade <
|
I recently had a similar need, this time to add a text annotation to an axvline that would stay at the top of the figure (say 5% from the edge) regardless of zooming. Perhaps an idea would be to provide a function to transform an Axes object into another overlaid, invisible (by default) Axes where one or both of the coordinates would use grid coordinates? Say,
(not that you necessarily want to do that) and my desired annotation would be
(I know about the However, this would not solve the OP's issue. On that note, I wonder whether it would be better to just add the functionality to |
This can be done easily with a blended transform of |
Can you point out to a code example? Thanks. |
It's kind of implicitly included in axes_zoom_effect and more explicitly mentioned at the end of fill_between_demo. But it's really a matter of supplying the transform for the text: from matplotlib.transforms import blended_transform_factory
trans = blended_transform_factory(ax.transData, ax.transAxes)
ax.text(123, # Data coordinates
0.95, # Fraction of Axes
'text',
transform=trans) or you can use from matplotlib.transforms import blended_transform_factory
trans = blended_transform_factory(ax.transData, ax.transAxes)
ax.annotate('text',
xy=123, 1), # (Data coordinates, Fraction of Axes)
xycoords=trans,
xytext=(2, -12), # (a few points to right, one line (default font size) below top (might need adjustment)
textcoords='offset points') |
I guess reading the transformations tutorial would have pointed me to I still think that my |
A solution with If |
@anntzer |
I am constantly missing such a function. I fit straight lines to data points and always have the problem of drawing the result of the fitting. Picking points on the line "far enough outside" such that the line is always visible is a pain. To add to the list of possible arguments for a function like abline i would like to add: pl.abline(a=[0, 0], direction=[0.70710678, 0.70710678]) # same line again, using 1 point and a direction vector |
What do you think of this implementation? http://stackoverflow.com/a/14348481/6605826 I would name it axdline to go with axhline and axvline. |
There are some issues with those details (like it should not be adding it's self to an axes or touching There should also be some discussion about how this should interact with non-linear scales on the axes. Only taking points at the edges will result in a 'linear on the screen' line independent of the scales. |
Also, this might as well be coupled to a general 'plot a function' artist. The simple case (evaluate at evenly spaced points) is not too bad. There was a PR to add this (#1143) this seems to have fallen by the way side (iirc it got hung up on smart sampling to deal with diverging functions). |
The transform I outlined above seems to work quite well, though I'm not sure about non-linear scales. |
It would be really nice to have some function that could draw arbitrarily angled straight lines on a graph, along the lines of ggplot's abline
for example, these might be ways to draw infinite lines:
These are all infinite lines (they extend to the bounds of the graph, regardless of where their specified points lie).
There are a few questions on stack overflow indicating how popular this might be, eg:
http://stackoverflow.com/questions/22104256/does-matplotlib-have-a-function-for-drawing-diagonal-lines-in-axis-coordinates
The text was updated successfully, but these errors were encountered: