Text geoms are useful for labeling plots. They can be used by themselves or in combination with other geoms.
from itertools import product
import pandas as pd
from lets_plot import *
from lets_plot import tilesets
from lets_plot.geo_data import *
from lets_plot.mapping import as_discrete
The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).
LetsPlot.setup_html()
geom_text()
/geom_label()
¶geom_text()
adds a text directly to the plot.geom_label()
adds a text directly to the plot with a rectangle behind the text, making it easier to read.label_plot = ggplot() + geom_label(x=0, y=0, label='Lorem ipsum', size=14)
text_plot = ggplot() + geom_text(x=0, y=0, label='Lorem ipsum')
gggrid([label_plot, text_plot]) + ggsize(600, 300)
Change additional parameters.
ggplot() + \
geom_label(x=0, y=0, label='Lorem ipsum', size=.9, size_unit='y', \
fill='#edf8e9', color='#238b45', fontface='bold', \
label_padding=1.0, label_r=0.5, label_size=2.0) + \
ggsize(500, 200)
Use different fonts for labels or text.
families = [
'Arial',
'Calibri',
'Garamond',
'Geneva',
'Georgia',
'Helvetica',
'Lucida Grande',
'Rockwell',
'Times New Roman',
'Verdana',
'sans-serif',
'serif',
'monospace',
]
ggplot() + \
geom_label(aes(y=list(range(len(families))), \
label=families, family=families), \
size=10, label_padding=0, label_r=0)
Add aesthetic parameters.
mpg_df = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv')
mpg_df.head()
Unnamed: 0 | manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | audi | a4 | 1.8 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
1 | 2 | audi | a4 | 1.8 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
2 | 3 | audi | a4 | 2.0 | 2008 | 4 | manual(m6) | f | 20 | 31 | p | compact |
3 | 4 | audi | a4 | 2.0 | 2008 | 4 | auto(av) | f | 21 | 30 | p | compact |
4 | 5 | audi | a4 | 2.8 | 1999 | 6 | auto(l5) | f | 16 | 26 | p | compact |
ggplot(mpg_df, aes('cty', 'hwy')) + geom_label(aes(label='fl'))
ggplot(mpg_df, aes('cty', 'hwy')) + \
geom_label(aes(label='fl', fill=as_discrete('cyl')), color='white')
label_format
Parameter¶The label_format
parameter specifies template for transforming value of the label
aesthetic to a string.
values_data = {
'y': list(range(5)),
'z': [1.0/3, 12.5/7, -22.5/11, 2.5/7, 31.67/1.77 ],
's': ['one', 'two', 'three', 'four', 'five']
}
Floating point numbers without formatting.
ggplot(values_data) + geom_text(aes(y='y', label='z'))
Floating point numbers with formatting.
ggplot(values_data) + geom_text(aes(y='y', label='z'), label_format='.3f')
Numbers formatted as percentile values
ggplot(values_data) + geom_text(aes(y='y', label='z'), label_format='.2%')
Number format as a part of a string pattern.
ggplot(values_data) + geom_text(aes(y='y', label='z'), label_format='Ttl: ${.2f} (B)')
String pattern without value formatting.
ggplot(values_data) + geom_text(aes(y='y', label='s'), label_format='"{}"')
data1 = {
'hjust': [0, 0.5, 1],
'vjust': [0, 0.5, 1],
'angle': [0, 30],
'label': ['first line\nsecond line']
}
df1 = pd.DataFrame(product(*data1.values()), columns=data1.keys())
p1 = ggplot(df1, aes(x='hjust', y='vjust')) + \
geom_point(size=3) + \
theme_light() + \
theme(panel_grid=element_blank())
p1_facets = p1 + \
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}°')
p1_facets + geom_text(aes(label='label', hjust='hjust', vjust='vjust', angle='angle'), size=9)
p1_facets + geom_label(aes(label='label', hjust='hjust', vjust='vjust', angle='angle'), size=9, alpha=0.5)
p11 = p1 + \
geom_text(aes(label='label', hjust='hjust', vjust='vjust'), \
size=8, lineheight=0.7) + \
ggtitle('lineheight=0.7')
p12 = p1 + \
geom_text(aes(label='label', hjust='hjust', vjust='vjust'), \
size=8, lineheight=2) + \
ggtitle('lineheight=2')
gggrid([p11, p12])
p13 = p1 + \
geom_label(aes(label='label', hjust='hjust', vjust='vjust'), \
size=8, alpha=0.5, lineheight=0.7) + \
ggtitle('lineheight=0.7')
p14 = p1 + \
geom_label(aes(label='label', hjust='hjust', vjust='vjust'), \
size=8, alpha=0.5, lineheight=2) + \
ggtitle('lineheight=2')
gggrid([p13, p14])
data2 = {
'hjust': [0, 0.5, 1],
'vjust': [0, 0.5, 1],
'angle': [0, 45, 90],
'text' : ['Text']
}
df2 = pd.DataFrame(product(*data2.values()), columns=data2.keys())
ggplot(df2, aes(x='hjust', y='vjust')) + \
geom_point(size=3) + \
geom_label(aes(label='text', angle='angle', hjust='hjust', vjust='vjust'), size=9) + \
facet_grid(y='angle') + \
scale_x_continuous(breaks=[0, 0.5, 1], expand=[0.1]) + \
scale_y_continuous(breaks=[0, 0.5, 1], expand=[0.0, 0.5]) + \
theme_classic() + \
theme(panel_border=element_rect(size=1))
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'))
position_nudge
¶p2 + geom_text(aes(label='y'), position=position_nudge(y=0.2))
nudge_y
Parameter¶p2 + geom_text(aes(label='y'), nudge_y=0.2)
inward
and outward
¶data3 = {
'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(data3, 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')
geom_text()
/geom_label()
¶GeoDataFrame is supported natively in the 'data' parameter for geom_label()
and geom_text()
.
gcoder = geocode_states('us-48').inc_res()
ggplot() + \
geom_polygon(aes(fill='state'), data=gcoder.get_boundaries(), \
show_legend=False, color='white', tooltips='none') + \
geom_label(aes(label='state'), data=gcoder.get_centroids(), size=6) + \
coord_map() + \
theme(axis='blank', panel_grid='blank') + \
scale_fill_brewer(name='state', palette='Dark2') + \
ggsize(900, 500)
cities_data = {
'city': ['New York City', 'Prague'],
'lon': [-73.7997, 14.418540],
'lat': [40.6408, 50.073658],
}
ggplot(cities_data, aes(x='lon', y='lat')) + \
geom_livemap(projection='epsg4326', tiles=tilesets.NASA_CITYLIGHTS_2012) + \
geom_path(color='white') + \
geom_label(aes(label='city'), \
size=8, hjust=0.5, vjust=0.5, fill='black', color='white', \
angle=5, label_padding=0.6, label_r=0.5, label_size=1.5) + \
geom_text(x=-35, y=50, label='Average flight time: 8 hrs 43 mins\n' +
'The shortest distance (air line): 4,082.79 mi', \
size=7, hjust=.5, lineheight=2, color='white')