Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: Possibilty to pass absolute values to axhline and axvline #15586

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

Closed
jakobjakobson13 opened this issue Nov 1, 2019 · 5 comments
Closed

Comments

@jakobjakobson13
Copy link

Dear developers,

I have a enhancement proposal: Could you please add the possibility to pass absolute values to axhline and axvline?

Background
I found it a bit inconvenient to draw horizontal or vertical lines to mark maxima or minima using the relative values if you are plotting actual data.

Code example

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
x = np.linspace(-1, 1)
def curve(x):
    return -np.random.rand()*(2)*x**2-np.random.rand() # the random numbers have just been introduced to avoid an easy guessing of the numbers in the following

fixed_curve = curve(x)
ax.plot(x, fixed_curve)
ylim = ax.get_ylim()
ytmp = (np.amax(fixed_curve) - ylim[0])/(ylim[1]-ylim[0])
ax.axvline(0, 0, ytmp) # better(?): ax.axvline(0, ylim[0], np.amax(fixed_curve), absolute = True)

Code explanation
In the example given above I want to mark to maximum with the coordinates (x, y) by a vertical line going from the maximum of the curve to the x axis to mark the value on the x axis. However, to exactly hit the maximum I first have to calculate the position of the maximum in respect the limits of the plot. Otherwise the line does either not "reach" the maximum or it "crosses" the maximum what would be counterintuitive.
This could be shortened by a parameter absolute or similar that allows you to draw a axvline or axhline from one absolute coordinate to another absolute coordinate.

What do you think about that suggestion?

Regards

Jakob

@timhoffm
Copy link
Member

timhoffm commented Nov 1, 2019

You can simply

ax.plot([x, x], [0, ymax])

Axvline is primarily intended to draw lines spanning the whole vertical range (or a fraction of it). Making it end on a specific data coordinate is not the intention.

@jklymak
Copy link
Member

jklymak commented Nov 1, 2019

@jakobjakobson13 Thanks for the interest in enhancing Matplotlib, but I agree with @timhoffm that this is already trivially easy. axhline is needed as its own method because it puts the line at the x value regardless of the ylimits. I'm going to close this as an active issue, but if you think we have misunderstood, or have more comments please feel free to make them.

@jklymak jklymak closed this as completed Nov 1, 2019
@jakobjakobson13
Copy link
Author

@jakobjakobson13 Thanks for the interest in enhancing Matplotlib, but I agree with @timhoffm that this is already trivially easy. axhline is needed as its own method because it puts the line at the x value regardless of the ylimits. I'm going to close this as an active issue, but if you think we have misunderstood, or have more comments please feel free to make them.

No thanks, it's fine that way.

@timhoffm
Copy link
Member

timhoffm commented Nov 1, 2019

Just for reference: If one want a line from a data position down to the x-axis, that's not really possible. One cannot mix coordinate systems here. As a workaround, one can draw the line to large negative values and exclude it from autoscaling by passing in_layout=False by the yet to be implemented method described in #15595.

@ImportanceOfBeingErnest
Copy link
Member

For a line with both ends in different coordinate systems also see ConnectionPatch, examplified in connect_simple01.

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

No branches or pull requests

4 participants