String Values of Legend Justification¶
The legend_justification parameter of theme() accepts the following string values: 'center' (the default), 'top', 'right', 'bottom', and 'left'.
In [1]:
from lets_plot import *
LetsPlot.setup_html()
In [2]:
data = {'x': [0, 0.4, 1], 'y': [0, 0.6, 1], 'Color': ['A', 'B', 'C']}
p = ggplot(data, aes('x', 'y', color='Color')) + \
geom_point() + \
theme(plot_title=element_text(hjust=0.5)) + \
ggsize(500, 300)
In [3]:
# The base plot with the legend in the center
p + ggtitle("Default legend position")
Out[3]:
In [4]:
# Align the legend to the top or bottom
gggrid([
p + theme(legend_justification="top") +
ggtitle("Align legend to the top"),
#
p + theme(legend_justification="bottom") +
ggtitle("Align legend to the bottom")])
Out[4]:
In [5]:
# When the legend is placed at the top or bottom, you can apply left or right justification
gggrid([
p + theme(legend_position="bottom", legend_justification="left") +
ggtitle("Align legend to the left"),
#
p + theme(legend_position="bottom", legend_justification="right") +
ggtitle("Align legend to the right")])
Out[5]: