Margins

The margins around the plot and text elements are controlled by the plot_margin parameter in theme() and the margin parameter in element_text() respectively.

Now the parameters plot_margin and margin accept a number or a list of numbers.

  • A number or list of one number is specified: the same margin it applied to all four sides.
  • A list of two numbers: the first margin applies to the top and bottom, the second - to the left and right.
  • A list of three numbers: the first margin applies to the top, the second - to the right and left, the third - to the bottom.
  • A list of four numbers: the margins are applied to the top, right, bottom and left in that order.

It is acceptable to use None for any side; in this case, the default side value for this element will be used.

In [1]:
import numpy as np

from lets_plot import *
In [2]:
LetsPlot.setup_html()
In [3]:
np.random.seed(42)
data = {'x': np.random.randint(10, size=100)}

p = ggplot(data, aes(x='x')) + \
    geom_bar() + \
    ggtitle("Bar Сhart") + \
    theme_light() + \
    theme(plot_background=element_rect(size = 4))

Plot without Margins

In [4]:
p
Out[4]:

Margins Around the Plot

In [5]:
gggrid([
    p + theme(plot_margin=40) + ggtitle("plot_margin=40 (all sides)"),
    p + theme(plot_margin=[40,20]) + ggtitle("plot_margin=[40,20]\n(top/bottom, left/right)"),
    p + theme(plot_margin=[40,20,10]) + ggtitle("plot_margin=[40,20,10]\n(top, left/right, bottom)"),
    p + theme(plot_margin=[40,10,10,20]) + ggtitle("plot_margin=[40,10,10,20]\n(top, right, bottom, left)")
], ncol=2)
Out[5]:

Margins Around the Text Element

In [6]:
p + theme(plot_title = element_text(margin=[40,None]),
          axis_title = element_text(margin=[20,20,None,None])) + \
    ggtitle("plot_title=[40,None] -> top/bottom=40,\n" +
            "axis_title=[20,20,None,None] -> top/right=20")
Out[6]: