Skip to content
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

Get x- and y- limits #906

Open
thistlillo opened this issue Jan 1, 2025 · 7 comments
Open

Get x- and y- limits #906

thistlillo opened this issue Jan 1, 2025 · 7 comments

Comments

@thistlillo
Copy link

In the API I see only methods for setting the x- and the y-limits.

Is there any way to get those limits?

@has2k1
Copy link
Owner

has2k1 commented Jan 2, 2025

The limits are determined automatically (plus some expansion) from the data. After the plot has been draw, you may be able to get the limits, but it is not part of the user API.

I think for better advice I need to know what you need the limits for.

@has2k1 has2k1 added the Question label Jan 2, 2025
@thistlillo
Copy link
Author

thistlillo commented Jan 3, 2025

Let's say that I generate a first chart where the limits are computed automatically.

Then I make another chart with different data, but want to use exactly the same axes limits of the first chart.

I cannot find a way to obtain this. Is there one?

Clearly, I could set the limits in the first chart manually and use the same limits in the second chart, but this is far from optimal - a lot of time to fine-tune the limits on the first chart.

@has2k1
Copy link
Owner

has2k1 commented Jan 4, 2025

A trick that will probably work forever is to use a function as if to modify the limits, but then just save the value and return the original limits.

from plotnine import *
from plotnine.data import mtcars

x_limits = (0, 0)

def save_x_limits(limits):
    global x_limits
    x_limits = limits
    return limits
    
p = (
    ggplot(mtcars, aes("wt", "mpg"))
    + geom_point()
    + scale_x_continuous(limits=save_x_limits)
)

# Draw the plot so that we get the limits
display(p) # or p.draw() or p.show()

print(x_limits) # your treasured limits!
print((min(mtcars["wt"]), max(mtcars["wt"]))) # Expected limits for this case

@thistlillo
Copy link
Author

Out of curiosity, is it so hard to add a function as get_limits?

@has2k1
Copy link
Owner

has2k1 commented Jan 7, 2025

Out of curiosity, is it so hard to add a function as get_limits?

Good idea.

@TerryGamon
Copy link

TerryGamon commented Jan 7, 2025

I had the same problem, because I wanted to place some annotation at exactly 90% of the y axis.
I solved it like this:

(basically the same as has2k1s solution)

p = (p9.ggplot(...))
fig = p.draw()
ax = fig.axes[0]

y_min, y_max = ax.get_ylim()
x_min, x_max = ax.get_xlim()

if you know the limits you can do something like this

my x axes is a date so

import matplotlib.dates as mdates
annotation_x1 = mdates.num2date(x_min + .05 *(x_max-x_min))
annotation_y = 0.9 *y_max


p_final = p + p9.annotate('text', 
                x= annotation_x1,
                y= annotation_y, 
                label='whatever,
                ha = 'left',
                size=16,
            )

of course a pure plotnine solution would be highly welcome!

@has2k1
Copy link
Owner

has2k1 commented Jan 7, 2025

I wanted to place some annotation at exactly 90% of the y axis.

This is a more common case that requires relative positioning and one should not even mess with the limits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants