You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: secondary_xaxis produces ticks at incorrect locations #29067
Your interpolation won't go lower than zero, but the main axis has negative values so the secondary axes is not one to one. Your forward and reverse should take care of negative values, probably leaving them the same or doing -x**2
Thanks for the quick reply @jklymak ! You're right about the negative values but I was hoping that the top x axis would not go negative and start at 0, same as the bottom x axis. As you suggest the following works code works by specifying the limits (note that if I don't specify the right xlim, the top x axis is still not correct). However it doesn't have the nice buffer of white space on the left and right that we are used to. Is there a way to retain that nice behavior with secondary_xaxis?
In order to properly handle the data margins, the mapping functions
(forward and inverse in this example) need to be defined beyond the nominal plot limits.
In the specific case of the numpy linear interpolation,
[numpy.interp](https://numpy.org/doc/stable/reference/generated/numpy.interp.html#numpy.interp), this
condition can be arbitrarily enforced by providing optional keyword arguments left, right such that values
outside the data range are mapped well outside the plot limits.
Feel free to carry on the discussion. However, I don't think there is much we can do here - you need to define forward and inverse to handle beyond 0 and 11 if you want margins to work. How exactly you do that is application dependent.
I'm not sure that's the only issue here: it seems that this functionality doesn't play nice with interp. The following simple example which does away with the negative complications works fine for the first set of functions and doesn't for the second set:
Ahh I see: that worked, thanks! Unless I'm mistaken about what the tutorial is trying to show, it might be suffering from the same problem I was running into. If there is interest I'd be happy to submit a PR to update the tutorial based on our discussion.
Activity
jklymak commentedon Nov 4, 2024
Your interpolation won't go lower than zero, but the main axis has negative values so the secondary axes is not one to one. Your forward and reverse should take care of negative values, probably leaving them the same or doing -x**2
jklymak commentedon Nov 4, 2024
Or just set_xlim(min=0)...
dkweiss31 commentedon Nov 4, 2024
Thanks for the quick reply @jklymak ! You're right about the negative values but I was hoping that the top x axis would not go negative and start at 0, same as the bottom x axis. As you suggest the following works code works by specifying the limits (note that if I don't specify the right xlim, the top x axis is still not correct). However it doesn't have the nice buffer of white space on the left and right that we are used to. Is there a way to retain that nice behavior with
secondary_xaxis?Producing

jklymak commentedon Nov 4, 2024
There is an explicit note about this in https://matplotlib.org/stable/gallery/subplots_axes_and_figures/secondary_axis.html#sphx-glr-gallery-subplots-axes-and-figures-secondary-axis-py:
jklymak commentedon Nov 4, 2024
Feel free to carry on the discussion. However, I don't think there is much we can do here - you need to define
forwardandinverseto handle beyond 0 and 11 if you want margins to work. How exactly you do that is application dependent.dkweiss31 commentedon Nov 4, 2024
I'm not sure that's the only issue here: it seems that this functionality doesn't play nice with
interp. The following simple example which does away with the negative complications works fine for the first set of functions and doesn't for the second set:first set:


second set (interp)
jklymak commentedon Nov 4, 2024
As above, you need to tell
np.interpwhat to do for x<2 and x>11.dkweiss31 commentedon Nov 4, 2024
Ahh I see: that worked, thanks! Unless I'm mistaken about what the tutorial is trying to show, it might be suffering from the same problem I was running into. If there is interest I'd be happy to submit a PR to update the tutorial based on our discussion.
jklymak commentedon Nov 4, 2024
Ah I see, I think you are correct. Fixing the tutorial would be super helpful!