Is there a way to prevent plotnine from printing user warnings when saving ggplot objects to a file?
Asked Answered
T

2

6

I'm building a simulation tool in python that outputs a number of plots using plotnine. However, for each individual plot I save, I get the following error messages:

C:\Users\tarca\Anaconda3\lib\site-packages\plotnine\ggplot.py:706: UserWarning: Saving 10 x 3 in image.
  from_inches(height, units), units))

C:\Users\tarca\Anaconda3\lib\site-packages\plotnine\ggplot.py:707: UserWarning: Filename: my_plot.png
  warn('Filename: {}'.format(filename))

I've already tried manually setting all of the arguments, and I've tried saving the files using both plot.save() and ggsave() - both yield the same result. If you search the error, the only thing that comes up is that the author of the following tutorial gets the same errors, though they are not addressed therein:

https://monashdatafluency.github.io/python-workshop-base/modules/plotting_with_ggplot/

To save the plots, I'm using code similar to:

plot.save(filename = 'my_plot.png', width = 10, height = 3, dpi = 300)

I'm hoping to be able to save the plots without generating any annoying messages that may confuse anyone using the program.

Trinee answered 23/4, 2019 at 6:25 Comment(0)
T
3

I'm not sure why this warning is still displayed in the tutorial you linked to, because as soon as I do

import warnings
warnings.filterwarnings('ignore')

as described there right at the beginning, too, the UserWarning which was printed before when saving a plot to disk is successfully suppressed.

Trichroism answered 23/4, 2019 at 11:17 Comment(1)
Yeah, that works for me too, but it suppresses all warnings - including ones that may be important. For the time being I'm turning warnings off and on before and after plotting, though I wish plotnine would resolve this as I'm not sure the purpose/value of the warnings.Trinee
C
2

Yes there is, just use:

fig2.save(fig_dir + "/figure2.png", width = w, height = h, verbose = False)

If you don't specify verbose = plotnine will always display a warning. See their GitHub module why.

Chare answered 17/3, 2020 at 5:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.