from lets_plot import *
import pandas as pd
LetsPlot.setup_html()
# 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}°'))
pf + geom_text(aes(label='label', hjust='hjust', vjust='vjust', angle='angle'), size=9)
pf + geom_label(aes(label='label', hjust='hjust', vjust='vjust', angle='angle'), size=9, alpha=0.5)
# Change lineheight
# lineheight = 0.7
p + geom_text(aes(label='label', hjust='hjust', vjust='vjust'), size=9, lineheight=0.7)
p + geom_label(aes(label='label', hjust='hjust', vjust='vjust'), size=9, alpha=0.5, lineheight=0.7)
# lineheight = 2.0
p + geom_text(aes(label='label', hjust='hjust', vjust='vjust'), size=9, lineheight=2)
p + geom_label(aes(label='label', hjust='hjust', vjust='vjust'), size=9, alpha=0.5, lineheight=2)
# 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'))
# Move text - use 'position_nudge'
p2 + geom_text(aes(label = 'y'), position=position_nudge(y=0.2))
# Same with 'nudge_y'
p2 + geom_text(aes(label = 'y'), nudge_y=0.2)
# 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)
p3 + geom_text(aes(label = 'text'), size = 8, hjust = 'inward', vjust = 'inward')
p3 + geom_text(aes(label = 'text'), size = 8, hjust = 'outward', vjust = 'outward')
# livemap: geom_label parameters and multiple lines support
data = {
'city': ['New York City', 'Prague'],
'lon': [-73.7997, 14.418540],
'lat': [40.6408, 50.073658],
}
ggplot(data, aes(x='lon', y='lat')) + \
geom_livemap(projection='epsg4326', 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=0, y=50,
label="Average flight time: 8 hrs 43 mins\n" +
"The shortest distance (air line): 4,082.79 mi",
size=7, hjust=1, lineheight=2, color='white')