Labels & Annotations¶
Add titles, axis labels, and annotations to your plots.
In [1]:
Copied!
import pandas as pd
import numpy as np
from ggplotly import *
# Sample data
np.random.seed(42)
df = pd.DataFrame({
'x': np.random.randn(50),
'y': np.random.randn(50),
'category': np.random.choice(['A', 'B', 'C'], 50),
'value': np.random.rand(50) * 10
})
import pandas as pd
import numpy as np
from ggplotly import *
# Sample data
np.random.seed(42)
df = pd.DataFrame({
'x': np.random.randn(50),
'y': np.random.randn(50),
'category': np.random.choice(['A', 'B', 'C'], 50),
'value': np.random.rand(50) * 10
})
labs()¶
The primary function for setting plot labels.
In [2]:
Copied!
ggplot(df, aes(x='x', y='y', color='category')) + geom_point(size=8) + \
labs(
title='Main Title',
subtitle='A subtitle with more detail',
x='X Axis Label',
y='Y Axis Label',
color='Legend Title',
caption='Data source: Sample data'
)
ggplot(df, aes(x='x', y='y', color='category')) + geom_point(size=8) + \
labs(
title='Main Title',
subtitle='A subtitle with more detail',
x='X Axis Label',
y='Y Axis Label',
color='Legend Title',
caption='Data source: Sample data'
)
Out[2]:
All labs() Parameters¶
| Parameter | Description |
|---|---|
title |
Main plot title |
subtitle |
Subtitle below title |
x |
X-axis label |
y |
Y-axis label |
z |
Z-axis label (3D plots) |
color |
Color legend title |
fill |
Fill legend title |
size |
Size legend title |
shape |
Shape legend title |
caption |
Caption at bottom-right |
ggtitle()¶
Quick way to set just the title.
In [3]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_point() + ggtitle('My Plot')
ggplot(df, aes(x='x', y='y')) + geom_point() + ggtitle('My Plot')
Out[3]:
In [4]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_point() + \
annotate('text', x=0, y=2, label='Important point!')
ggplot(df, aes(x='x', y='y')) + geom_point() + \
annotate('text', x=0, y=2, label='Important point!')
Out[4]:
Rectangle Highlight¶
In [5]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_point() + \
annotate('rect', xmin=-1, xmax=1, ymin=-1, ymax=1, fill='yellow', alpha=0.3)
ggplot(df, aes(x='x', y='y')) + geom_point() + \
annotate('rect', xmin=-1, xmax=1, ymin=-1, ymax=1, fill='yellow', alpha=0.3)
Out[5]:
Line Segment¶
In [6]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_point() + \
annotate('segment', x=-2, y=-2, xend=2, yend=2, color='red')
ggplot(df, aes(x='x', y='y')) + geom_point() + \
annotate('segment', x=-2, y=-2, xend=2, yend=2, color='red')
Out[6]:
Reference Lines¶
In [7]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_point() + \
annotate('hline', y=0, color='red', linetype='dash') + \
annotate('vline', x=0, color='blue', linetype='dot')
ggplot(df, aes(x='x', y='y')) + geom_point() + \
annotate('hline', y=0, color='red', linetype='dash') + \
annotate('vline', x=0, color='blue', linetype='dot')
Out[7]:
Annotation Parameters¶
| Parameter | Description |
|---|---|
x, y |
Position coordinates |
xend, yend |
End coordinates (segments) |
xmin, xmax, ymin, ymax |
Rectangle bounds |
label |
Text content |
color |
Color |
fill |
Fill color |
size |
Size (text or point) |
alpha |
Transparency |
hjust, vjust |
Horizontal/vertical alignment |
Controlling Legends¶
guides()¶
Control legend display for each aesthetic.
In [8]:
Copied!
# Hide color legend
ggplot(df, aes(x='x', y='y', color='category')) + geom_point(size=8) + guides(color='none')
# Hide color legend
ggplot(df, aes(x='x', y='y', color='category')) + geom_point(size=8) + guides(color='none')
Out[8]:
guide_legend()¶
Customize legend appearance.
In [9]:
Copied!
ggplot(df, aes(x='x', y='y', color='category')) + geom_point(size=8) + \
guides(color=guide_legend(title='Categories', ncol=2))
ggplot(df, aes(x='x', y='y', color='category')) + geom_point(size=8) + \
guides(color=guide_legend(title='Categories', ncol=2))
Out[9]:
guide_colorbar()¶
Customize continuous color bar.
In [10]:
Copied!
ggplot(df, aes(x='x', y='y', color='value')) + geom_point(size=8) + \
guides(color=guide_colorbar(title='Values', direction='horizontal'))
ggplot(df, aes(x='x', y='y', color='value')) + geom_point(size=8) + \
guides(color=guide_colorbar(title='Values', direction='horizontal'))
Out[10]:
In [11]:
Copied!
# Find max point
max_idx = df['y'].idxmax()
max_x, max_y = df.loc[max_idx, 'x'], df.loc[max_idx, 'y']
(ggplot(df, aes(x='x', y='y', color='category'))
+ geom_point(size=8)
+ annotate('text', x=max_x, y=max_y + 0.3, label='Maximum')
+ annotate('rect', xmin=-1, xmax=1, ymin=-1, ymax=1, fill='gray', alpha=0.2)
+ labs(
title='Scatter Plot with Annotations',
subtitle='Highlighting a region of interest',
x='X Variable',
y='Y Variable',
caption='Source: Sample Data'
))
# Find max point
max_idx = df['y'].idxmax()
max_x, max_y = df.loc[max_idx, 'x'], df.loc[max_idx, 'y']
(ggplot(df, aes(x='x', y='y', color='category'))
+ geom_point(size=8)
+ annotate('text', x=max_x, y=max_y + 0.3, label='Maximum')
+ annotate('rect', xmin=-1, xmax=1, ymin=-1, ymax=1, fill='gray', alpha=0.2)
+ labs(
title='Scatter Plot with Annotations',
subtitle='Highlighting a region of interest',
x='X Variable',
y='Y Variable',
caption='Source: Sample Data'
))
Out[11]:
Clean Plot with Hidden Legend¶
In [12]:
Copied!
(ggplot(df, aes(x='x', y='y', color='category'))
+ geom_point(size=8)
+ theme_minimal()
+ theme(legend_position='none')
+ labs(title='Clean Plot'))
(ggplot(df, aes(x='x', y='y', color='category'))
+ geom_point(size=8)
+ theme_minimal()
+ theme(legend_position='none')
+ labs(title='Clean Plot'))
Out[12]: