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
Allow specifying x-data range for ax.set_ymargin() #19955
Comments
|
I think a feature like this sits right on the edge of what matplotlib could/should do. It certainly seems feasible, but I imagine there might be a lot of nuances that might make this tricky to get right. Thinking about this more generally, what should the API look like for 3d plots? What about polar plots? Would it present complications for cartopy? And, once implemented, should we adopt this sort of behavior for interactive zooming (or perhaps it should be a special mode?) The idea isn't limited to set_x/ymargin(). Perhaps it could be considered for set_x/ylim(), and any other limit setting approaches. IIRC, glueviz has something along these lines. This idea has the potentially to be immensely useful, but needs to be carefully thought out. |
|
Yes I agree this is on the edge. Its not super hard for the user to narrow their own field of view down to the y values corresponding to the x values between 0 and 1: import numpy as np
import matplotlib.pyplot as plt
x = np. arange(-2, 2, 0.1)
np.random.seed(19680801)
y = np.cumsum(np.random.randn(len(x)))
fig, ax = plt.subplots()
ax.plot(x, y)
ind = ((x>=0) & (x<=1))
ax.set_xlim(0, 1)
ax.set_ylim(y[ind].min(), y[ind].max())
plt.show()If you have more than one dataset, then just run a for-loop. Matplotlib could provide an API like this. Whether it is worth it or not is another issue.
See #9890; Indeed this feature seems better as |
|
I have also stumbled into this discussion: In particular, I would also be very interested in autoscaling the ylimits based on the view in the currently configured xlim. In the plotting behaviour of software I have used in the past, this is typically the default, but I now understand apparently this is not the default in Matlab, and hence also not in matplotlib. I think though that it would be nice to enable this functionality, via kwargs, and maybe an rcParam (don't know if the latter makes sense? need to look more at how the rcParams work) Proposed option?One possibly logical place to incorporate this could be an attribute / kwarg of the autoscale function: For example, one could consider a parameter Related question about querying autoscaling attributesIn relation to this, I also am a bit lost on how to query autoscaling attributes of a plot in MPL. There is a function to set this |
|
I another (related) place where this might be logical is the I see that this also already make use of some |
|
As an aside, I also assumed this would be the default behavior of matplotlib when using set_xlim(), and was surprised to find out that it was not. |
|
+1 |
|
I agree with edmundsj. It is not normal, and it is not well connected to the reality of how people use the software to graph data. I have yet to find a suitable workaround. The api, relim(), autscale_view() also do not work in so far as respecting the range set by xlim. And set_ylim() with min() and max() does not provide the "nice limits" like those chosen by auto scaling. Manually selecting the subset of the data before calling the graphics API, might be a crude workaround, but it can be complicated for the user. That some suggest that implementing this would somehow present corner cases, seems more than a little surprising. If you can autoscale the full range of data, you can surely autoscale the subset selected by xlim. And, I'm sorry that no matter how many times it is flagged as wontfix or similar, we have to consider matplotlib as broken in this regard until either it is fixed or some api is added to provide nice y limits after changing xlim. Otherwise, matplotlib is certainly nice software. I hope you do fix this. The bug gives me lots of problems preparing figures. |
|
I don't think anyone marked this as "won't fix". Just no one has proposed an API and opened a PR. But from the discussion above I hope it is clear that making this the default behaviour would be very hard. |
|
I don't think it would be so hard. If you have acess to the x range and data, it's a few lines of code. And api wise, it could easily be an rc configurable option. Maybe if I have time i will dig into the xlim code. |
|
The api proposal is above and clear to me (And i thought it was marked won't fiix at some point. But may be losing track on the different issues I've commented on..) In any case, the emotion suggests that this is a feature that would be useful to users. |
|
I did a pull of the code but I'm afraid I won't have the time to figure out autoscale_view and set_xlim source right now (who knows in a couple of months) to the point where I would feel confident making a pull request But bascially something like this: where here I make an argument for the use case here: |
|
Need to loop over all the visible lines.
…On Wed, Feb 9, 2022, 3:44 PM Gary Steele ***@***.***> wrote:
I did a pull of the code but I'm afraid I won't have the time to figure
out autoscale_view and set_xlim source right now (who knows in a couple of
months) to the point where I would feel confident making a pull request
But bascially something like this:
def my_xlim(x1,x2,adjust_ylim = False):
plt.xlim(x1,x2)
if adjust_ylim:
mask = (x>x1)*(x<x2)
y_plot = y[mask]
y_plot_min = np.min(y_plot)
y_plot_max = np.max(y_plot)
delta_y = y_plot_max - y_plot_min
y_plot_max += delta_y/10
y_plot_min -= delta_y/10
plt.ylim(y_plot_min, y_plot_max)
I make an argument for the use case here:
https://github.com/gsteele13/gary-misc-notebooks/blob/master/prototype%20matplotlib%20modified%20xlim.ipynb
—
Reply to this email directly, view it on GitHub
<#19955 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJGUVUONE3Y6LSIECNX6KNLU2LG3BANCNFSM424IYPTA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you commented.Message ID:
***@***.***>
|
|
The use cases include setting xlim to bracket a subset with a smaller range
than that of the full data.
One example is data from instruments with ccd sensors where the first or
last group of pixels is masked.
Another is the decay tail following an impulse.
Another is the small oscillations at long time in a damped oscillator
Another is the noise in almost any data set in the baseline or any other
region of low slope.
…On Wed, Feb 9, 2022, 3:44 PM Gary Steele ***@***.***> wrote:
I did a pull of the code but I'm afraid I won't have the time to figure
out autoscale_view and set_xlim source right now (who knows in a couple of
months) to the point where I would feel confident making a pull request
But bascially something like this:
def my_xlim(x1,x2,adjust_ylim = False):
plt.xlim(x1,x2)
if adjust_ylim:
mask = (x>x1)*(x<x2)
y_plot = y[mask]
y_plot_min = np.min(y_plot)
y_plot_max = np.max(y_plot)
delta_y = y_plot_max - y_plot_min
y_plot_max += delta_y/10
y_plot_min -= delta_y/10
plt.ylim(y_plot_min, y_plot_max)
I make an argument for the use case here:
https://github.com/gsteele13/gary-misc-notebooks/blob/master/prototype%20matplotlib%20modified%20xlim.ipynb
—
Reply to this email directly, view it on GitHub
<#19955 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJGUVUONE3Y6LSIECNX6KNLU2LG3BANCNFSM424IYPTA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you commented.Message ID:
***@***.***>
|
|
Closing as a duplicate of #2123. |

Problem
I'm trying to create a plot which "zooms in" on a subset of a large dataset (I am plotting the entire dataset because it's much more convenient than clipping the underlying dataset). I can do this pretty easily for the x-axis with ax.set_xlim(), because I know what the x-limits of my data should be. However, I don't know what the y-limits should be, and I can't use ax.set_ymargin() because it computes the y-margin for the entire dataset, not the zoomed portion of the dataset. I don't know ahead of time what the min/max values of my dataset will be, so I'm stuck.
Proposed Solution
Add an option to ax.set_ymargin specifying a subset of the x-data range over which to apply the margin. For example:
would "zoom in" in y on the portion of the data that exists in the x-range [0, 1], and apply a 10% margin to the data that exists over that range.
The text was updated successfully, but these errors were encountered: