-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
the limits of axes are inexact with mplot3d #18052
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
Comments
The relevant code is matplotlib/lib/mpl_toolkits/mplot3d/axis3d.py Line 188 in 7e95303
which auto-expands the bounds. We actually want auto-expansion for automatically determined bounds, but not for user bounds. 👉 Things to do:
|
I'd be interested in looking into this |
Great @neilZon ! Please let us know if you have any questions (either here or on https://gitter.im/matplotlib/matplotlib ) |
A default plot immediately sets both matplotlib/lib/matplotlib/axes/_axes.py Line 1608 in bc97294
matplotlib/lib/matplotlib/axes/_base.py Lines 787 to 793 in bc97294
A draw calls _unstale_viewLim(): matplotlib/lib/matplotlib/axes/_base.py Line 2859 in bc97294
The matplotlib/lib/matplotlib/axes/_base.py Lines 766 to 778 in bc97294
This inner function is called for each, which does the (much more detailed) 2D bound calculations: matplotlib/lib/matplotlib/axes/_base.py Lines 2696 to 2762 in bc97294
If you call, e.g., matplotlib/lib/matplotlib/axes/_base.py Lines 3565 to 3567 in bc97294
Meanwhile, |
We are a group of students that have looked into this for a while as part of a course. Thank you @kentcr for some excellent documentation of the 2D-case. It helped us a lot.
Actually, it seems that this is already done in the matplotlib/lib/mpl_toolkits/mplot3d/axis3d.py Lines 188 to 189 in 7e95303
The
This problem is not correlating with setting the |
It's a y-tick being drawn. If you clear the y-ticks, it disappears: import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection=Axes3D.name)
ax.set_yticks([])
ax.set_xlim(0, 0.8)
plt.show() Further, if you remove the first tick in each axis here... matplotlib/lib/mpl_toolkits/mplot3d/axis3d.py Line 221 in 7e95303
e.g. ticks = self._update_ticks()[1:] ...it fixes this problem, but you lose the gridline where the background planes intersect: I'd initially hoped to just change
matplotlib/lib/mpl_toolkits/mplot3d/axis3d.py Lines 8 to 9 in 7e95303
matplotlib/lib/matplotlib/axis.py Line 1972 in 7e95303
matplotlib/lib/matplotlib/axis.py Line 1020 in 7e95303
It does seem possible to just change the ticks just for the y-axis, but the obvious ways don't seem very elegant to me. Of course, it's also rather fishy that the 3D YAxis and ZAxis classes are inheriting from the 2D XAxis class, so maybe elegance is a lost cause without more significant changes. It may also be that the ticks are actually fine and it's some lower-level rendering issue, but I haven't dug that far yet. Edit: I should say it's actually clear that the |
I take an example from
https://stackoverflow.com/questions/23951230/python-axis-limit-in-matplotlib
In mplot3d, when you set, for example,
set_xlim(0, 0.8)
, the outcome has an extra likeset_xlim(0 - eps, 0.8 + eps)
, where eps is related todeltas
.matplotlib/lib/mpl_toolkits/mplot3d/axis3d.py
Line 187 in 6e76411
To show the exact lims, you have to change
deltas
to, for example,0 * (maxs - mins) / 12.
You need something to set exact lims without touching the inside of matplotlib.
Code for reproduction
Actual outcome

Matplotlib version
The problem was discussed many times in STACK OVERFLOW:
https://stackoverflow.com/questions/23951230/python-axis-limit-in-matplotlib
https://stackoverflow.com/questions/46380464/matplotlib-3d-axis-bounds-always-too-large-doesnt-set-lims-correctly
https://stackoverflow.com/questions/16488182/removing-axes-margins-in-3d-plot
The text was updated successfully, but these errors were encountered: