Using AstroPy with matplotlib, I get a warning to call grid(False) first due to use of plt.colorbar()
Asked Answered
S

1

6

Trying to run this code from AstroPy:docs

    import matplotlib.pyplot as plt
    from astropy.visualization import astropy_mpl_style
    plt.style.use(astropy_mpl_style)
    
    from astropy.utils.data import get_pkg_data_filename
    from astropy.io import fits
    
    image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')
    fits.info(image_file)
    
    image_data = fits.getdata(image_file, ext=0)
    
    print(image_data.shape)
    
    plt.figure()
    plt.imshow(image_data, cmap='gray')
    
    plt.colorbar()

I get a warning:

line 19 plt.colorbar() 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.

I tried calling plt.grid(False), but continue to get this warning/error. Does anyone know how to resolve this?

Sileas answered 17/11, 2021 at 5:24 Comment(0)
S
3

For users that may have stumbled upon this issue searching the Matplotlib 3.5 deprecation warning message. Any style that has axes.grid True may trigger this issue. Generically, it can overriden in plt.rcParams['axes.grid'] = False but care needs to be taken about where and when this is set.

UPDATE 1/19/2022

I've submitted a bug report to astropy about this warning message. See GitHub issue

UPDATE 1/26/2022

Matplotlib has fixed this issue in matplotlib/matplotlib#22285. I've confirmed with the development version of Matplotlib 3.6 that the warning no longer appears in the code in question.

addressing the issue from the question

The issue is that astropy_mpl_style has the grid on as the default and this triggers a deprecation warning message about calling plt.grid(False) before calling plt.colorbar() in Matplotlib version 3.5. This warning message occurs despite doing what it says because the style takes precedence over the function calls.

One work around is to override the astropy_mpl_style grid setting and prevent the grid from turning on before plt.colorbar() is called.

astropy_mpl_style['axes.grid'] = False

Here is how it folds into the code to prevent the warning message from Matplotlib version 3.5

import matplotlib.pyplot as plt
from astropy.visualization import astropy_mpl_style
astropy_mpl_style['axes.grid'] = False
plt.style.use(astropy_mpl_style)

from astropy.utils.data import get_pkg_data_filename
from astropy.io import fits

image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')
fits.info(image_file)

image_data = fits.getdata(image_file, ext=0)

print(image_data.shape)

plt.figure()
plt.imshow(image_data, cmap='gray')

plt.colorbar()
Salivate answered 5/1, 2022 at 23:56 Comment(2)
it just removed the warning not a solutionObligate
Azhar I've looked into this a little more deeply and put in a bug report to astropy. Thanks for poking meSalivate

© 2022 - 2024 — McMap. All rights reserved.