Themes¶
Themes control the overall appearance of your plots - backgrounds, fonts, gridlines, and more.
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(100),
'y': np.random.randn(100),
'category': np.random.choice(['A', 'B', 'C'], 100),
'value': np.random.rand(100) * 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(100),
'y': np.random.randn(100),
'category': np.random.choice(['A', 'B', 'C'], 100),
'value': np.random.rand(100) * 10
})
In [2]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_point() + theme_minimal()
ggplot(df, aes(x='x', y='y')) + geom_point() + theme_minimal()
Out[2]:
theme_classic¶
Classic look with axis lines, no gridlines.
In [3]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_point() + theme_classic()
ggplot(df, aes(x='x', y='y')) + geom_point() + theme_classic()
Out[3]:
theme_dark¶
Dark background theme.
In [4]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_point() + theme_dark()
ggplot(df, aes(x='x', y='y')) + geom_point() + theme_dark()
Out[4]:
theme_ggplot2¶
Replicates R's ggplot2 default theme.
In [5]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_point() + theme_ggplot2()
ggplot(df, aes(x='x', y='y')) + geom_point() + theme_ggplot2()
Out[5]:
theme_bbc¶
BBC News style visualizations.
In [6]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_point() + theme_bbc()
ggplot(df, aes(x='x', y='y')) + geom_point() + theme_bbc()
Out[6]:
theme_nytimes¶
New York Times style visualizations.
In [7]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_point() + theme_nytimes()
ggplot(df, aes(x='x', y='y')) + geom_point() + theme_nytimes()
Out[7]:
In [8]:
Copied!
# Move legend position
ggplot(df, aes(x='x', y='y', color='category')) + geom_point() + theme(legend_position='bottom')
# Move legend position
ggplot(df, aes(x='x', y='y', color='category')) + geom_point() + theme(legend_position='bottom')
Out[8]:
In [9]:
Copied!
# Hide legend
ggplot(df, aes(x='x', y='y', color='category')) + geom_point() + theme(legend_position='none')
# Hide legend
ggplot(df, aes(x='x', y='y', color='category')) + geom_point() + theme(legend_position='none')
Out[9]:
Text Elements¶
In [10]:
Copied!
# Title styling
ggplot(df, aes(x='x', y='y')) + geom_point() + \
labs(title='My Plot') + \
theme(plot_title=element_text(size=20, color='darkblue'))
# Title styling
ggplot(df, aes(x='x', y='y')) + geom_point() + \
labs(title='My Plot') + \
theme(plot_title=element_text(size=20, color='darkblue'))
Out[10]:
In [11]:
Copied!
# Axis labels
ggplot(df, aes(x='x', y='y')) + geom_point() + \
theme(axis_title=element_text(size=14, color='red'))
# Axis labels
ggplot(df, aes(x='x', y='y')) + geom_point() + \
theme(axis_title=element_text(size=14, color='red'))
Out[11]:
Background Elements¶
In [12]:
Copied!
# Panel background
ggplot(df, aes(x='x', y='y')) + geom_point() + \
theme(panel_background=element_rect(fill='lightgray'))
# Panel background
ggplot(df, aes(x='x', y='y')) + geom_point() + \
theme(panel_background=element_rect(fill='lightgray'))
Out[12]:
Gridlines¶
In [13]:
Copied!
# Major gridlines
ggplot(df, aes(x='x', y='y')) + geom_point() + \
theme(panel_grid_major=element_line(color='gray', width=0.5))
# Major gridlines
ggplot(df, aes(x='x', y='y')) + geom_point() + \
theme(panel_grid_major=element_line(color='gray', width=0.5))
Out[13]:
Combining Themes¶
Start with a base theme and customize:
In [14]:
Copied!
(
ggplot(df, aes(x='x', y='y', color='category'))
+ geom_point()
+ theme_minimal()
+ theme(
legend_position='bottom',
plot_title=element_text(size=18)
)
+ labs(title='My Customized Plot')
)
(
ggplot(df, aes(x='x', y='y', color='category'))
+ geom_point()
+ theme_minimal()
+ theme(
legend_position='bottom',
plot_title=element_text(size=18)
)
+ labs(title='My Customized Plot')
)
Out[14]:
Theme Reference¶
| Theme | Description |
|---|---|
theme_default |
Default ggplotly theme |
theme_minimal |
Minimal, clean theme |
theme_classic |
Classic with axis lines |
theme_dark |
Dark background |
theme_ggplot2 |
R's ggplot2 default |
theme_bbc |
BBC News style |
theme_nytimes |
NYT style |
theme_custom |
Build from scratch |
theme() Parameters¶
| Parameter | Description |
|---|---|
legend_position |
Legend position ('right', 'left', 'top', 'bottom', 'none') |
legend_show |
Show/hide legend (bool) |
plot_title |
Title text styling |
plot_subtitle |
Subtitle styling |
axis_title |
Both axis titles |
axis_title_x |
X-axis title |
axis_title_y |
Y-axis title |
axis_text |
Both axis tick labels |
axis_text_x |
X-axis tick labels |
axis_text_y |
Y-axis tick labels |
legend_title |
Legend title styling |
legend_text |
Legend text styling |
panel_background |
Plot area background |
plot_background |
Full plot background |
panel_grid_major |
Major gridlines |
panel_grid_minor |
Minor gridlines |