Physical Metrics Parameters for ggsave()

Parameters dpi, w, h and unit can be used to configure export to a raster image with required physical metrics. Aspect ratio must be maintained manually between ggsize() and ggsave(). Please note that using these parameters will disable the scale parameter.

In [1]:
import numpy as np

from lets_plot import *
In [2]:
LetsPlot.setup_html()
In [3]:
np.random.seed(12)
data = dict(
     cond=np.repeat(['A', 'B'], 200),
     rating=np.concatenate((np.random.normal(0, 1, 200), np.random.normal(1, 1.5, 200)))
)

p = ggplot(data, aes(x='rating', fill='cond')) + ggsize(800, 400) + \
     geom_density(color='dark_green', alpha=.7) + scale_fill_brewer(type='seq') + \
     theme(panel_grid_major_x='blank')
p
Out[3]:

Default

In [4]:
_ = ggsave(p, 'plot_ggsave.png')

ggsave()

In [5]:
_ = ggsave(p, 'plot_ggsave_96dpi.png', w=8, h=4, unit='in', dpi=96)
In [6]:
_ = ggsave(p, 'plot_ggsave_300dpi.png', w=8, h=4, unit='in', dpi=300)

to_png()

In [7]:
_ = p.to_png('plot_to_png_96dpi.png', w=8, h=4, unit='in', dpi=96)
In [8]:
_ = p.to_png('plot_to_png_300dpi.png', w=8, h=4, unit='in', dpi=300)

gggrid()

In [9]:
g = gggrid([
    p,
    p + coord_flip()
]) + ggsize(1000, 300)
g
Out[9]:
In [10]:
_ = ggsave(g, 'grid_ggsave_96dpi.png', w=10, h=3, unit='in', dpi=96)
In [11]:
_ = ggsave(g, 'grid_ggsave_300dpi.png', w=10, h=3, unit='in', dpi=300)