Skip to content

[Bug]: Some styles trigger pcolormesh grid deprecation #21723

Description

@prisae

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

Activity

  1. changed the title [-][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[/+] on Nov 23, 2021
  2. QuLogic commented on Nov 23, 2021

    @QuLogic
    Member

    I cannot reproduce this in a script or in Jupyter Notebook. Maybe you have a custom matplotlibrc or style setting?

  3. prisae commented on Nov 23, 2021

    @prisae
    Author

    Good point, and I tried. By default it indeed does not happen. However, it happens if you use, e.g., plt.style.use('bmh') or plt.style.use('ggplot').

  4. jklymak commented on Nov 23, 2021

    @jklymak
    Member

    OK, so this has nothing to do with colorbar explicitly, and the same warning pops up for pcolromesh by itself:

    import matplotlib.pyplot as plt
    import numpy as np
    plt.style.use('ggplot')
    
    fig, ax = plt.subplots()
    # 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)
    
    surf = ax.pcolormesh(X, Y, Z)
    plt.show()
    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.
      surf = ax.pcolormesh(X, Y, Z)
    

    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.

  5. changed the title [-][Bug]: `plot_surface` followed by `fig.colorbar` results in DeprecationWarning; suggested fix does not work[/-] [+][Bug]: Some styles trigger pcolormesh grid deprecation[/+] on Nov 23, 2021
  6. added
    API: changesChanges to the public API, typically requiring deprecation.
    and removed on Nov 23, 2021
  7. timhoffm commented on Nov 23, 2021

    @timhoffm
    Member

    [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:

    • depending on style we may or may not have grids on; plus maybe explicitly set by the user.
    • pcolormesh with a grid on top is not wanted most of the time.

    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:

    • All shipped styles with grid use axes.axisbelow: True and thus the grid vanishes behind the mesh
    • The default style has axes.axisbelow: line but no grid

    Problem 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=True parameter. That way, users can deactivate the behavior if needed. This has still a small discoverability problem, but bearable:

    1. I run my code and the grid vanishes
    2. I debug and find that pcolormesh removes it
    3. I check the docs and find remove_grid

    Note: 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.

  8. phollox-arch commented on Nov 24, 2021

    @phollox-arch

    Hello,
    I'm not sure if I'm adding to this discussion, but I'm seeing the same warning. I'm importing seaborn and calling sns.set() and sns.set_palette('bright')

  9. jklymak commented on Nov 24, 2021

    @jklymak
    Member
    • I feel it was a misfeature that we ever removed the grid in the first place just for this artists type.
    • I don't see a way to sensibly deprecate this since it is not something the user is actively doing. So we can
      1. stop the behaviour without deprecation, or
      2. live with it.

    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.

  10. jklymak commented on Nov 24, 2021

    @jklymak
    Member

    ... added to the weekly dev call to discuss, unless we can come to consensus here.

  11. prisae commented on Jan 4, 2022

    @prisae
    Author

    Was there anything decided in this regard? I see the same problem appearing in v3.5.1 as well.

  12. jklymak commented on Jan 4, 2022

    @jklymak
    Member

    @prisae No I don't think there was a discussion. I'll re-add to this weeks call. Thanks for the ping...

  13. added this to the v3.6.0 milestone on Jan 4, 2022
  14. 4 remaining items

  15. tacaswell commented on Jan 13, 2022

    @tacaswell
    Member

    The consensus on the call is to go with option 3 (leave the warning, but only when the grid is above the colormesh).

  16. jklymak commented on Jan 14, 2022

    @jklymak
    Member

    re-open because the above is still not implemented to my knowledge.

  17. AzharUddinSheikh commented on Jan 19, 2022

    @AzharUddinSheikh

    plt.rcParams['axes.grid'] = False
    this stop the warning

  18. pllim commented on Jan 20, 2022

    @pllim

    Hello. I think this might be affecting astropy too. Please see astropy/astropy#12753 . Thanks!

  19. self-assigned this
    on Jan 21, 2022
  20. added 4 commits that reference this issue on Jan 22, 2022
    6408397
    9e6d723
    e67271c
    b996b4f
  21. modified the milestones: v3.6.0, v3.5.2 on Jan 25, 2022
  22. added a commit that references this issue on Jan 26, 2022
    df85ec0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

API: changesChanges to the public API, typically requiring deprecation.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions