Scales¶
Scales control how data values are mapped to visual properties and how axes are displayed.
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.rand(100) * 100,
'y': np.random.rand(100) * 100,
'category': np.random.choice(['A', 'B', 'C'], 100),
'value': np.random.rand(100) * 10
})
# Time series data
ts_df = pd.DataFrame({
'date': pd.date_range('2024-01-01', periods=100, freq='D'),
'value': np.cumsum(np.random.randn(100)) + 50
})
# Log scale data
log_df = pd.DataFrame({
'x': np.logspace(0, 3, 50),
'y': np.logspace(0, 3, 50) * np.random.uniform(0.5, 2, 50)
})
import pandas as pd
import numpy as np
from ggplotly import *
# Sample data
np.random.seed(42)
df = pd.DataFrame({
'x': np.random.rand(100) * 100,
'y': np.random.rand(100) * 100,
'category': np.random.choice(['A', 'B', 'C'], 100),
'value': np.random.rand(100) * 10
})
# Time series data
ts_df = pd.DataFrame({
'date': pd.date_range('2024-01-01', periods=100, freq='D'),
'value': np.cumsum(np.random.randn(100)) + 50
})
# Log scale data
log_df = pd.DataFrame({
'x': np.logspace(0, 3, 50),
'y': np.logspace(0, 3, 50) * np.random.uniform(0.5, 2, 50)
})
In [2]:
Copied!
# Basic continuous scale with limits
ggplot(df, aes(x='x', y='y')) + geom_point() + scale_x_continuous(limits=(0, 100))
# Basic continuous scale with limits
ggplot(df, aes(x='x', y='y')) + geom_point() + scale_x_continuous(limits=(0, 100))
Out[2]:
Log Scales¶
In [3]:
Copied!
ggplot(log_df, aes(x='x', y='y')) + geom_point() + scale_x_log10() + scale_y_log10()
ggplot(log_df, aes(x='x', y='y')) + geom_point() + scale_x_log10() + scale_y_log10()
Out[3]:
Date/Time Axes¶
In [4]:
Copied!
# Date axis
ggplot(ts_df, aes(x='date', y='value')) + geom_line() + scale_x_date()
# Date axis
ggplot(ts_df, aes(x='date', y='value')) + geom_line() + scale_x_date()
Out[4]:
Interactive Scales¶
In [5]:
Copied!
# Range slider for zooming
ggplot(ts_df, aes(x='date', y='value')) + geom_line() + scale_x_rangeslider()
# Range slider for zooming
ggplot(ts_df, aes(x='date', y='value')) + geom_line() + scale_x_rangeslider()
Out[5]:
In [6]:
Copied!
ggplot(df, aes(x='x', y='y', color='category')) + geom_point(size=5) + \
scale_color_manual(values=['red', 'blue', 'green'])
ggplot(df, aes(x='x', y='y', color='category')) + geom_point(size=5) + \
scale_color_manual(values=['red', 'blue', 'green'])
Out[6]:
In [7]:
Copied!
# With named mapping
ggplot(df, aes(x='x', y='y', color='category')) + geom_point(size=5) + \
scale_color_manual(values={'A': 'red', 'B': 'blue', 'C': 'green'})
# With named mapping
ggplot(df, aes(x='x', y='y', color='category')) + geom_point(size=5) + \
scale_color_manual(values={'A': 'red', 'B': 'blue', 'C': 'green'})
Out[7]:
Color Gradients¶
In [8]:
Copied!
# Two-color gradient
ggplot(df, aes(x='x', y='y', color='value')) + geom_point(size=5) + \
scale_color_gradient(low='blue', high='red')
# Two-color gradient
ggplot(df, aes(x='x', y='y', color='value')) + geom_point(size=5) + \
scale_color_gradient(low='blue', high='red')
Out[8]:
ColorBrewer Palettes¶
In [9]:
Copied!
# Qualitative palette
ggplot(df, aes(x='x', y='y', color='category')) + geom_point(size=5) + \
scale_color_brewer(palette='Set1')
# Qualitative palette
ggplot(df, aes(x='x', y='y', color='category')) + geom_point(size=5) + \
scale_color_brewer(palette='Set1')
Out[9]:
Available palettes:
- Sequential: Blues, Greens, Oranges, Reds, Purples, Greys, BuGn, BuPu, GnBu, OrRd, PuBu, PuRd, RdPu, YlGn, YlGnBu, YlOrBr, YlOrRd
- Diverging: BrBG, PiYG, PRGn, PuOr, RdBu, RdGy, RdYlBu, RdYlGn, Spectral
- Qualitative: Accent, Dark2, Paired, Pastel1, Pastel2, Set1, Set2, Set3
Viridis Palette¶
In [10]:
Copied!
# Viridis for heatmaps
x = np.arange(10)
y = np.arange(10)
X, Y = np.meshgrid(x, y)
Z = np.sin(X / 2) * np.cos(Y / 2)
tile_df = pd.DataFrame({'x': X.flatten(), 'y': Y.flatten(), 'z': Z.flatten()})
ggplot(tile_df, aes(x='x', y='y', fill='z')) + geom_tile() + scale_fill_viridis_c()
# Viridis for heatmaps
x = np.arange(10)
y = np.arange(10)
X, Y = np.meshgrid(x, y)
Z = np.sin(X / 2) * np.cos(Y / 2)
tile_df = pd.DataFrame({'x': X.flatten(), 'y': Y.flatten(), 'z': Z.flatten()})
ggplot(tile_df, aes(x='x', y='y', fill='z')) + geom_tile() + scale_fill_viridis_c()
Out[10]:
Fill Scales¶
In [11]:
Copied!
# Fill scales for bars
bar_df = pd.DataFrame({'category': ['A', 'B', 'C'], 'value': [25, 40, 35]})
ggplot(bar_df, aes(x='category', y='value', fill='category')) + geom_col() + \
scale_fill_brewer(palette='Set2')
# Fill scales for bars
bar_df = pd.DataFrame({'category': ['A', 'B', 'C'], 'value': [25, 40, 35]})
ggplot(bar_df, aes(x='category', y='value', fill='category')) + geom_col() + \
scale_fill_brewer(palette='Set2')
Out[11]:
Shape Scale¶
In [12]:
Copied!
ggplot(df, aes(x='x', y='y', shape='category')) + geom_point(size=8) + \
scale_shape_manual(values=['circle', 'square', 'triangle-up'])
ggplot(df, aes(x='x', y='y', shape='category')) + geom_point(size=8) + \
scale_shape_manual(values=['circle', 'square', 'triangle-up'])
Out[12]:
Size Scale¶
In [13]:
Copied!
ggplot(df, aes(x='x', y='y', size='value')) + geom_point() + \
scale_size(range=(1, 15))
ggplot(df, aes(x='x', y='y', size='value')) + geom_point() + \
scale_size(range=(1, 15))
Out[13]:
Axis Limits¶
Quick ways to set axis limits:
In [14]:
Copied!
# Using xlim/ylim
ggplot(df, aes(x='x', y='y')) + geom_point() + xlim(0, 50) + ylim(0, 50)
# Using xlim/ylim
ggplot(df, aes(x='x', y='y')) + geom_point() + xlim(0, 50) + ylim(0, 50)
Out[14]:
In [15]:
Copied!
# Using coord_cartesian (doesn't clip data, just zooms)
ggplot(df, aes(x='x', y='y')) + geom_point() + coord_cartesian(xlim=(20, 80), ylim=(20, 80))
# Using coord_cartesian (doesn't clip data, just zooms)
ggplot(df, aes(x='x', y='y')) + geom_point() + coord_cartesian(xlim=(20, 80), ylim=(20, 80))
Out[15]:
Scale Reference¶
| Scale | Description |
|---|---|
scale_x_continuous |
Continuous x-axis |
scale_y_continuous |
Continuous y-axis |
scale_x_log10 |
Log10 x-axis |
scale_y_log10 |
Log10 y-axis |
scale_x_date |
Date x-axis |
scale_x_datetime |
DateTime x-axis |
scale_x_rangeslider |
Interactive range slider |
scale_x_rangeselector |
Range selector buttons |
scale_color_manual |
Manual color mapping |
scale_color_gradient |
2-color gradient |
scale_color_brewer |
ColorBrewer palettes |
scale_fill_manual |
Manual fill mapping |
scale_fill_gradient |
Fill gradient |
scale_fill_brewer |
ColorBrewer fill |
scale_fill_viridis_c |
Viridis fill |
scale_shape_manual |
Manual shape mapping |
scale_size |
Size scaling |