Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

569
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda