-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
[Bug]: ax.invert_xaxis() and ax.invert_yaxis() both flip the X axis #21369
Comments
The 3D wrappers in |
Something like the following fixes the binding problem: diff --git a/lib/mpl_toolkits/mplot3d/axis3d.py b/lib/mpl_toolkits/mplot3d/axis3d.py
index 7d6110c25a..cce815eaa6 100644
--- a/lib/mpl_toolkits/mplot3d/axis3d.py
+++ b/lib/mpl_toolkits/mplot3d/axis3d.py
@@ -32,7 +32,7 @@ def tick_update_position(tick, tickxs, tickys, labelpos):
tick.gridline.set_data(0, 0)
-class Axis(maxis.XAxis):
+class Axis(maxis.Axis):
"""An Axis class for the 3D plots."""
# These points from the unit cube make up the x, y and z-planes
_PLANES = (
@@ -516,21 +516,21 @@ class Axis(maxis.XAxis):
# Use classes to look at different data limits
-class XAxis(Axis):
+class XAxis(Axis, maxis.XAxis):
get_view_interval, set_view_interval = maxis._make_getset_interval(
"view", "xy_viewLim", "intervalx")
get_data_interval, set_data_interval = maxis._make_getset_interval(
"data", "xy_dataLim", "intervalx")
-class YAxis(Axis):
+class YAxis(Axis, maxis.YAxis):
get_view_interval, set_view_interval = maxis._make_getset_interval(
"view", "xy_viewLim", "intervaly")
get_data_interval, set_data_interval = maxis._make_getset_interval(
"data", "xy_dataLim", "intervaly")
-class ZAxis(Axis):
+class ZAxis(Axis, maxis.XAxis):
get_view_interval, set_view_interval = maxis._make_getset_interval(
"view", "zz_viewLim", "intervalx")
get_data_interval, set_data_interval = maxis._make_getset_interval( but it breaks the Y ticks, probably because of some assumption elsewhere. |
Labeling this as a good first issue because there is a proposed patch and it does not involve any API design choices, however this is not a good issue for someone new-to-programming as it will require chasing through some multiple inheritance issues ;) . |
@QuLogic Could you please tell me how it breaks the y-ticks? I tried out the patch but couldn't figure it out. |
The padding is wrong, and sometimes extra grid lines are drawn that shouldn't be. Run the tests and you will see what breaks. |
I examined this and think that it is actually the tick position which is appearing to throw the padding off. For x-axis, the tick position can be either If you adopt the fix suggested by @QuLogic (fixing inheritance in 3D axes) and add
A solution to this may be to create 3D yaxes with different tick and label positions. However, the needed position for the axis will likely depend on the viewing angle of the 3D plot. It does perhaps make sense though, to set the default positions for the 3D y axis to work well with the default viewing angle. I'd like to submit a pull request to fix this bug, but I may need some additional help on how best to set the position of the ticks for the y axis. Currently, the y-axis for 3D plots looks like this:
Would an advisable approach be to override the This is what I have tried:
and after creating the plots, using the command Image attached showing that using |
There are a couple of possible gotcha's in the mplot3D code base. The most 😱 of which is that we do some type promotion where we init the 2D version of the object and then reset the My next debugging step here would be to override
to sort out where it is being called and to add either breakpoints or prints to the new |
This commit corrects the axis which the 3D y axis inherits from. The y-axis on 3D plots initially inherited from the x axis object, rather than a y axis object. This resulted in a bug where the x axis (instead of the y) was inverted for 3D plots when was called. After inheritance correction, correct behavior was achieved with inversion, but the tick marks were incorrectly placed. The default location for tick marks on the y-axis is now set to be right, rather than left. Resolves matplotlib#21369
Thanks for the tip @tacaswell - it was going through the constructor, but a lot more happens after that. I added the command at a point after the axes are created, may be a bandaid fix. PR opened, lots of tests failing at the moment. |
Is this issue still open? I've been looking for a starting point for contribution to open source |
Hello ! So, here's what I've found about the problem that prevents you I tried fixing it by making an init method in the "YAxis" The only way I found to bypass this problem is by doing this : -adding this method in "matplotlib\axis.py" -> YTick class :
-adding this line in "matplotlib\axis.py" -> YAxis class in the init :
-rewriting the _get_tick method in "matplotlib\axis.py" -> YAxis class :
-adding these lines in "mpl_toolkits\mplot3d\axis3d.py" -> YAxis class :
This is in fact just checking if the projection is 2D or 3D, in order to |
@coding-krtek - You can check your solution by running the unit tests. If you have pytest ( In my PR for this issue #21424, I fixed the axis inheritance and have changed the behavior in the constructor of I can't speak for the repo maintainers, but my thought would be that you do not want to change parts of the code (for example |
Um I'll be honest, I was just changing the matplotlib files directly, and I don't know how cloned repositories or virtual environments work. Yes, I changed the inheritance Edit
And this time "everything" (except 11 tests) works, both inheritance and ticks. It's really a duct-tape solution but I don't think there's a way of fixing this issue by changing only the YAxis class and nothing else. |
Hey @coding-krtek - if you'd like to contribute your solution to matplotlib you'll need to fork the repository and make a pull request. This way, others can see and try out your changes and they might be accepted into the main matplotlib library. You can join the gitter and ask to be put in the "incubator" channel to get help with this - https://gitter.im/matplotlib/matplotlib |
I also noticed that |
For those in need for a workaround: I discovered that the following lines actually do invert the yaxis: ylim = ax.get_ylim()
ax.set_yticks( ax.get_yticks() )
ax.set_ylim(ylim[::-1]) |
Closed by #21442. |
Bug summary
The ax.invertxaxis() and ax.invert_yaxis() function both produce the same output, a scatterplot with a flipped X axis.
Code for reproduction
Actual outcome
No inversions:
X Inversion:
Y Inversion:
Z Inversion behaves as expected:
Expected outcome
I would expect the y axis to be inverted
Operating system
No response
Matplotlib Version
3.2.2
Matplotlib Backend
No response
Python version
No response
Jupyter version
No response
Other libraries
mpl_toolkits.mplot3d
Installation
No response
Conda channel
No response
The text was updated successfully, but these errors were encountered: