from lets_plot import *
import re
LetsPlot.setup_html()
def text_plot(color, alpha):
rgba_alpha = None
m = re.match(r'rgba\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*,\s*([0-9.]+)\s*\)', color)
if m:
rgba_alpha = float(m.group(1))
label = f"{rgba_alpha}\n{alpha}"
return (
ggplot() +
geom_text(
x=0, y=0,
label=label,
size=24,
color=color,
alpha=alpha
) +
theme_void() +
theme(panel_grid_major=element_line(size=4)) +
ggsize(300, 200)
)
gggrid([
text_plot(color='rgba(255,0,0,0)', alpha=None),
text_plot(color='rgba(255,0,0,0)', alpha=0),
text_plot(color='rgba(255,0,0,0)', alpha=0.3),
text_plot(color='rgba(255,0,0,0)', alpha=1),
#
text_plot(color='rgba(255,0,0,0.3)', alpha=None),
text_plot(color='rgba(255,0,0,0.3)', alpha=0),
text_plot(color='rgba(255,0,0,0.3)', alpha=0.3),
text_plot(color='rgba(255,0,0,0.3)', alpha=1),
#
text_plot(color='rgba(255,0,0,0.6)', alpha=None),
text_plot(color='rgba(255,0,0,0.6)', alpha=0),
text_plot(color='rgba(255,0,0,0.6)', alpha=0.3),
text_plot(color='rgba(255,0,0,0.6)', alpha=1),
#
text_plot(color='rgba(255,0,0,1)', alpha=None),
text_plot(color='rgba(255,0,0,1)', alpha=0),
text_plot(color='rgba(255,0,0,1)', alpha=0.3),
text_plot(color='rgba(255,0,0,1)', alpha=1)
], ncol=4) + ggsize(600, 800) + ggtitle('If set, the alpha aesthetic overrides the alpha from the color')
# element_text
(ggplot({'x': [1, 2, 3], 'y': [2, 4, 3]}, aes('x', 'y'))
+ geom_point(size=5)
+ geom_line()
+ labs(
title='Plot Title',
subtitle='Plot Subtitle',
caption='Plot Caption',
tag='A'
)
+ theme(
plot_title=element_text(color="rgba(255,0,0, 0.3)", size=28),
plot_subtitle=element_text(color="rgba(0,255,0, 0.3)", size=24),
plot_caption=element_text(color="rgba(0,255,255, 0.3)", size=22),
plot_tag=element_text(color="rgba(255,0,255, 0.3)", size=30, face='bold')
)
)
# geom_tile
(
ggplot({'x': [1, 2], 'y': [1, 1]})
+ geom_tile(
aes(x='x', y='y'),
color='rgba(0,0,0,0.2)',
size=2,
alpha = 0.3,
linetype=4
)
)
df = {
'x': [0, 1, 2, 3],
'y': [0, 0, 0, 0],
'label': ['0%', '25%', '50%', '100%'],
'color': ['#ff000000', '#ff000040', '#ff000080', '#ff0000ff'],
}
(
ggplot(df, aes('x', 'y'))
+ geom_text(aes(label='label', color='color'), size=16)
+ scale_color_identity()
+ ggsize(500, 180)
)