In [1]:
from lets_plot import *

import pandas as pd
LetsPlot.setup_html()
In [2]:
# Support of multiple lines

d = {
    'hjust': [0, 0.5, 1],
    'vjust': [0, 0.5, 1],
    'angle': [0, 30],
    'label': ["first line\nsecond line"]
}

from itertools import product
td = pd.DataFrame(product(*d.values()), columns=d.keys())

p = (ggplot(td, aes(x='hjust', y='vjust')) + 
    geom_point(size=3) +
    theme_light() + theme(panel_grid=element_blank())
)

pf = (p + scale_x_continuous(breaks=[0, 0.5, 1]) +
    scale_y_continuous(breaks=[0, 0.5, 1], expand=[0.4]) +
    facet_grid(x='angle', x_format='{d}°'))
In [3]:
pf + geom_text(aes(label='label', hjust='hjust', vjust='vjust', angle='angle'), size=9)
Out[3]:
In [4]:
pf + geom_label(aes(label='label', hjust='hjust', vjust='vjust', angle='angle'), size=9, alpha=0.5)
Out[4]:
In [5]:
# Change lineheight

# lineheight = 0.7

p + geom_text(aes(label='label', hjust='hjust', vjust='vjust'), size=9, lineheight=0.7)
Out[5]:
In [6]:
p + geom_label(aes(label='label', hjust='hjust', vjust='vjust'), size=9, alpha=0.5, lineheight=0.7)
Out[6]:
In [7]:
# lineheight = 2.0

p + geom_text(aes(label='label', hjust='hjust', vjust='vjust'), size=9, lineheight=2)
Out[7]:
In [8]:
p + geom_label(aes(label='label', hjust='hjust', vjust='vjust'), size=9, alpha=0.5, lineheight=2)
Out[8]:
In [ ]:
 
In [9]:
# Adjust position by nudging a given offset

p2 = ggplot({'x': ['a', 'b', 'c'], 'y': [1.2, 3.4, 2.5]}, aes('x', 'y')) + geom_point(size=4) + ggsize(500, 300)

p2 + geom_text(aes(label = 'y'))
Out[9]:
In [10]:
# Move text - use 'position_nudge'

p2 + geom_text(aes(label = 'y'), position=position_nudge(y=0.2))
Out[10]:
In [11]:
# Same with 'nudge_y'

p2 + geom_text(aes(label = 'y'), nudge_y=0.2)
Out[11]:
In [12]:
# Justification: 'inward' and 'outward'

df = {
   'x' : [1, 1, 2, 2, 1.5],
   'y' : [1, 2, 1, 2, 1.5],
   'text': ["bottom-left", "top-left", "bottom-right", "top-right", "center"]
}


p3 = ggplot(df, aes('x', 'y')) + geom_point(size=4) + ggsize(500, 300)
p3 + geom_text(aes(label = 'text'), size = 8)
Out[12]:
In [13]:
p3 + geom_text(aes(label = 'text'), size = 8, hjust = 'inward', vjust = 'inward')
Out[13]:
In [14]:
p3 + geom_text(aes(label = 'text'), size = 8, hjust = 'outward', vjust = 'outward')
Out[14]:
In [15]:
# livemap: geom_label parameters and multiple lines support
data = {
    'city': ['New York City', 'Singapore'],
    'lon': [-73.7997, 104.0012],
    'lat': [40.6408, 1.3256],
}


ggplot(data, aes(x='lon', y='lat')) + \
    geom_livemap(geodesic=False, projection='epsg4326', symbol='point', color='white',
               tiles=maptiles_lets_plot(theme='dark')) + \
    geom_path(color='white') + \
    geom_label(aes(label='city'), fill='black', color='white',
               size=8, angle=10, hjust=0, vjust=1,
               label_padding=0.6, label_r=0.5, label_size=1.5) + \
    geom_text(
        x=40, y=50,
        label="First flight: November 9th 2020\n" + 
              "Flight distance: 15,349 km\n" + 
              "Flight time: 18 Hours, 50 Minutes\n" + 
              "Aircraft: Airbus A350-900ULR",
        size=7, hjust=0, lineheight=2, color='white')
Out[15]: