lets-plot icon indicating copy to clipboard operation
lets-plot copied to clipboard

Add ggsave (or save) method to PlotSpec class

Open schakalakka opened this issue 3 years ago • 3 comments

Add a save convenience method to the PlotSpec class.

Now we have the ggsave(plot, ...) method.

For convencience it would be great if we would add a ggsave() or save() method to class itself. Sometimes in a notebook you don't want to assign your plot to a variable and use it like (ggplot(...) + geom_bar(...) + ggtitle(...)).show()

If you are then satisfied with your plot you could change .show() to .save(filename=...)

(Rough) Solution:

def save(self, filename, path, iframe): 
    ggsave(self, filename, path, iframe)

Maybe the same could be done for GGBunch.

On another note: What is the reasoning behind the distinction between path and filename? Usually you could enter the whole absolute or relative path to a filename argument and save it there.

schakalakka avatar Aug 29 '22 07:08 schakalakka

Sure, we can add such method to the class. But did you consider a different workflow: without show(). If "plot expression" is the last one in cell, then after execution of the cell the result (plot object) is stored (by the Python interpreter) in a variable named "_". Then you can write ggsave(_, "myplot.svg") Seems like this takes even less efforts than using show() and then replacing .show() to .save(filename=...). What do you think?

alshan avatar Sep 11 '22 20:09 alshan

What is the reasoning behind the distinction between path and filename?

This idea was taken from from ggplot2 (R). We're always trying to preserve the similarity to the ggplot2 API where possible.

alshan avatar Sep 11 '22 20:09 alshan

Sure, we can add such method to the class. But did you consider a different workflow: without show(). If "plot expression" is the last one in cell, then after execution of the cell the result (plot object) is stored (by the Python interpreter) in a variable named "_". Then you can write ggsave(_, "myplot.svg") Seems like this takes even less efforts than using show() and then replacing .show() to .save(filename=...). What do you think?

Thank you for the tip, I will try it.

schakalakka avatar Sep 13 '22 19:09 schakalakka

Hi, added in v4.2.0 : example notebook.

alshan avatar Dec 28 '23 19:12 alshan