Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 584
Arcadius Fox Avatar asked Apr 23 '19 06:04

Arcadius Fox


People also ask

Is Plotnine same as Ggplot?

plotnine is based on ggplot2 from the R programming language, so if you have a background in R, then you can consider plotnine as the equivalent of ggplot2 in Python. In this tutorial, you'll learn how to: Install plotnine and Jupyter Notebook. Combine the different elements of the grammar of graphics.

How do I save an object in ggplot2?

In most cases ggsave() is the simplest way to save your plot, but sometimes you may wish to save the plot by writing directly to a graphics device. To do this, you can open a regular R graphics device such as png() or pdf() , print the plot, and then close the device using dev.

How do I save a Ggplot plot?

You can either print directly a ggplot into PNG/PDF files or use the convenient function ggsave() for saving a ggplot. The default of ggsave() is to export the last plot that you displayed, using the size of the current graphics device. It also guesses the type of graphics device from the extension.

What are the default dimensions that Ggsave ()` saves an image as?

2.1. The default size of the saved image is equal to the size of Plots pane (the “graphics device”) in RStudio, which can be found with dev. size() . Notice that the result of dev. size() and the message we receive when saving the plot with ggsave() give the same dimensions.


2 Answers

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.

like image 132
SpghttCd Avatar answered Oct 07 '22 19:10

SpghttCd


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.

like image 44
Jordy Avatar answered Oct 07 '22 19:10

Jordy