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

Support for dpi parameter in `ggsave()`

Open Zaf4 opened this issue 2 years ago • 3 comments

Currently, there is no support for dpi parameter in ggsave() . This comes handy when saving to png and it is present in the R version of ggplot.

Zaf4 avatar Aug 07 '23 17:08 Zaf4

Hi! Could you please describe your use case, how would you use the DPI parameter? What exactly should this parameter affect (set meta info in a PNG file, image resolution, plot size)? We have the scale parameter that works in a similar way. Can this parameter work in your cases? Thank you!

IKupriyanov-HORIS avatar Nov 17 '23 11:11 IKupriyanov-HORIS

Hi,

DPI is for resolution. You're right scale works in a very similar way. Using pixels makes more sense of course. But in some cases, mostly for printing, people have to use inches or centimeters for dimensions which are then converted to pixels according to given DPI parameter. This eliminates the need for manual calculation to satisfy those requirements. Here are the ggplot2 and matplotlib implementation for reference.

So, using DPI also somewhat necessitates the use of dimensions in terms of inches, centimeters etc., In terms of conversion, 1 pixel = 1/96th of an inch or 1 point = 1/72nd of an inch (source). Addition of cm, inch could also help people export vector images with the desired sizes too.

In a sense, when the dimensions are provided, they should override scale and be converted into pixels for both vector images and png images. For png images, DPI parameter is also required for resolution in such cases. Default value for dpi in matplotlib and ggplot2 are 72 and 300, respectively.

Hope this helps!

Zaf4 avatar Nov 18 '23 12:11 Zaf4

This helps a lot. Thank you!

IKupriyanov-HORIS avatar Nov 21 '23 17:11 IKupriyanov-HORIS

More details on how DPI and size work in matplotlib: image

Source

IKupriyanov-HORIS avatar Mar 11 '24 21:03 IKupriyanov-HORIS

Hi guys, I happen to need to ggsave() a plot with particular width and height in cm, as a .pdf. After struggling a little I found that ggsave() does save plots in 96 dpi, is that right ? And I also found that the scale option works for pdf too ! (the doc still indicates that it only works when exporting in png.)

I agree with @Zaf4, dpi option would be nice !

It makes me think that the possibility to directly use cm or pixels in ggsave() would be awesome too !

egayer avatar Mar 26 '24 13:03 egayer

Hi @Zaf4 , @egayer Currently, one point is not well understood: the effects observed in matplotlib when the plot size is constant and DPI is varied. Specifically, the sizes of plot elements (font size, line width, etc.) are scaled up with increasing DPI, as shown in @IKupriyanov-HORIS's post above. Is this the desired behavior?

alshan avatar Mar 26 '24 20:03 alshan

Specifically, the sizes of plot elements (font size, line width, etc.) are scaled up with increasing DPI

That's one of things I don't like about matplotlib. I think DPI should not change the size of plot elements at all and higher figsize should not result in proportionally smaller plot elements. DPI should only effect the resolution of raster images like scale.

Basically, size parameters such as cm, inch should effect pdf, svg dimensions and dpi should dictate the raster image resolution.

Zaf4 avatar Mar 27 '24 00:03 Zaf4

Fixed in v4.3.1

See demo.

IKupriyanov-HORIS avatar Apr 17 '24 18:04 IKupriyanov-HORIS

once again, great job ;) thanks you guys, it works perfectly !

egayer avatar Apr 18 '24 08:04 egayer