Repository navigation
Labels
API: changesChanges to the public API, typically requiring deprecation.Changes to the public API, typically requiring deprecation.
Milestone
Description
Bug summary
Calling plot_surface followed by fig.colorbar results in the following deprecation warning:
Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5
and will be removed two minor releases later; please call grid(False) first.
However, setting ax.grid(False) first does not help, the Warning is still shown.
Code for reproduction
Edit: As noted further down in the discussion: The warning does not appear for the default style. But if you load, e.g., plt.style.use('bmh'), you will see the warning.
# https://matplotlib.org/stable/gallery/mplot3d/surface3d.html
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import numpy as np
plt.style.use('bmh') # Edit: One of the styles with which you will see the warning
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
# A StrMethodFormatter is used automatically
ax.zaxis.set_major_formatter('{x:.02f}')
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()Actual outcome
MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated
since 3.5 and will be removed two minor releases later; please call grid(False) first.
fig.colorbar(surf, shrink=0.5, aspect=5)
Expected outcome
No Warning or a warning with a fix that works.
Additional information
Works fine for matplotlib<3.5.
Operating system
Linux
Matplotlib Version
3.5.0
Matplotlib Backend
module://ipykernel.pylab.backend_inline
Python version
3.88
Jupyter version
6.4.0
Installation
conda
Reacted by Joel Croteau, Martin Staadecker, Azhar Uddin Sheikh, Quant Girl, Abhinav Sharma, Alfredo Canziani and kamilkhan52
Metadata
Metadata
Assignees
Labels
API: changesChanges to the public API, typically requiring deprecation.Changes to the public API, typically requiring deprecation.
Activity
[-][Bug]: `plt_surface` followed by `fig.colorbar` results in DeprecationWarning; suggested fix does not work[/-][+][Bug]: `plot_surface` followed by `fig.colorbar` results in DeprecationWarning; suggested fix does not work[/+]QuLogic commentedon Nov 23, 2021
I cannot reproduce this in a script or in Jupyter Notebook. Maybe you have a custom
matplotlibrcor style setting?prisae commentedon Nov 23, 2021
Good point, and I tried. By default it indeed does not happen. However, it happens if you use, e.g.,
plt.style.use('bmh')orplt.style.use('ggplot').jklymak commentedon Nov 23, 2021
OK, so this has nothing to do with colorbar explicitly, and the same warning pops up for
pcolromeshby itself:This came in in #15604 (@anntzer) In that PR we stopped pcolormesh from removing the grid lines automatically, which was the old behaviour.
OTOH, I guess I am not clear why we are deprecating this, or if it is even possible to do so cleanly. Its impossible for pcolormesh to know if the user expects the grids to stay or go.
[-][Bug]: `plot_surface` followed by `fig.colorbar` results in DeprecationWarning; suggested fix does not work[/-][+][Bug]: Some styles trigger pcolormesh grid deprecation[/+]timhoffm commentedon Nov 23, 2021
[tl/dr] I needed this write-down to find out what do to. Go to Solution if you don't want to follow the details.
I'll try to summarize the situation.
History
Basic conflict:
Before #15604 the canonical resolution is that the mesh trumps the grid and pcolormesh deactivates the grid.
This is what you want in many cases, but not always, e.g. The mesh doesn't fill the whole Axes (#15600 which brought up the topic) or a user could have explicitly activated a grid.
Conceptually, auto-remove-grid through a plotting function is questionable. That's beyond their responsibility.
So #15604 deprecated the behavior.
We can get away with this because it doesn't change appearance for most standard cases:
axes.axisbelow: Trueand thus the grid vanishes behind the meshaxes.axisbelow: linebut no gridProblem here
The problem is that these cases still see the (irrelevant) deprecation message.
Solution
I think we can remove most of the deprecation annoyance by not warning in case of
axes.axisbelow: True.Alternative
For completeness only, after writing this I decided that actually removing grid manipulation is the correct thing to do.
imshow()doesn't remove the grid either.One could also reconsider that grid-removal is somewhat meaningful for pcolormesh, but make it configurable via a
remove_grid=Trueparameter. That way, users can deactivate the behavior if needed. This has still a small discoverability problem, but bearable:remove_gridNote: I don't think that defaulting to False would make sense. With either case one would change the default only in rare cases. But adding a parameter that optionally executes a command for rare cases, does not make sense if I can do the same with a simple
ax.grid(False)in the next line.phollox-arch commentedon Nov 24, 2021
Hello,
I'm not sure if I'm adding to this discussion, but I'm seeing the same warning. I'm importing
seabornand callingsns.set()andsns.set_palette('bright')jklymak commentedon Nov 24, 2021
I personally prefer 1 as I consider this a bug, but can understand why some would think this was a feature and would prefer 2.
jklymak commentedon Nov 24, 2021
... added to the weekly dev call to discuss, unless we can come to consensus here.
prisae commentedon Jan 4, 2022
Was there anything decided in this regard? I see the same problem appearing in
v3.5.1as well.jklymak commentedon Jan 4, 2022
@prisae No I don't think there was a discussion. I'll re-add to this weeks call. Thanks for the ping...
4 remaining items
tacaswell commentedon Jan 13, 2022
The consensus on the call is to go with option 3 (leave the warning, but only when the grid is above the colormesh).
jklymak commentedon Jan 14, 2022
re-open because the above is still not implemented to my knowledge.
AzharUddinSheikh commentedon Jan 19, 2022
plt.rcParams['axes.grid'] = False
this stop the warning
pllim commentedon Jan 20, 2022
Hello. I think this might be affecting
astropytoo. Please see astropy/astropy#12753 . Thanks!Don't warn on grid removal deprecation is grid is hidden
Don't warn on grid removal deprecation is grid is hidden
Don't warn on grid removal deprecation is grid is hidden
Don't warn on grid removal deprecation is grid is hidden
subfigure _axstack property