'Using AstroPy with matplotlib, I get a warning to call grid(False) first due to use of plt.colorbar()
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?
Solution 1:[1]
For users that may have stumbled upon this issue searching the Matplotlib 3.5 deprecation warning message. Any style that has
axes.gridTrue may trigger this issue. Generically, it can overriden inplt.rcParams['axes.grid'] = Falsebut 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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
