Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

561
Vistas
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?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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()
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda