Aesthetics¶
Aesthetics map columns in your data to visual properties of the plot. They are specified using the aes() function.
Basic Aesthetics¶
| Aesthetic | Description | Example |
|---|---|---|
x |
X-axis position | aes(x='column') |
y |
Y-axis position | aes(y='column') |
color |
Point/line color | aes(color='category') |
fill |
Fill color (bars, areas) | aes(fill='category') |
size |
Point/line size | aes(size='value') |
shape |
Point shape | aes(shape='category') |
alpha |
Transparency | aes(alpha='value') |
group |
Grouping variable | aes(group='category') |
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),
'species': 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=50, freq='D'),
'value': np.cumsum(np.random.randn(50)) + 50
})
ts_df = ts_df.set_index('date') # Set date as index, removes it from columns
# Bar data
bar_df = pd.DataFrame({
'category': ['A', 'B', 'C', 'D'],
'count': [25, 40, 35, 30]
})
# Line data with groups
line_df = pd.DataFrame({
'x': np.tile(np.arange(10), 3),
'y': np.random.randn(30),
'id': np.repeat(['A', 'B', 'C'], 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),
'species': 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=50, freq='D'),
'value': np.cumsum(np.random.randn(50)) + 50
})
ts_df = ts_df.set_index('date') # Set date as index, removes it from columns
# Bar data
bar_df = pd.DataFrame({
'category': ['A', 'B', 'C', 'D'],
'count': [25, 40, 35, 30]
})
# Line data with groups
line_df = pd.DataFrame({
'x': np.tile(np.arange(10), 3),
'y': np.random.randn(30),
'id': np.repeat(['A', 'B', 'C'], 10)
})
In [2]:
Copied!
# Color varies by 'species' column
ggplot(df, aes(x='x', y='y', color='species')) + geom_point()
# Color varies by 'species' column
ggplot(df, aes(x='x', y='y', color='species')) + geom_point()
Out[2]:
Setting (inside geom)¶
Fixed value for all points:
In [3]:
Copied!
# All points are blue
ggplot(df, aes(x='x', y='y')) + geom_point(color='blue')
# All points are blue
ggplot(df, aes(x='x', y='y')) + geom_point(color='blue')
Out[3]:
Index as Aesthetic¶
You can use the DataFrame index as an aesthetic value:
In [4]:
Copied!
# Auto-populate: if x is omitted, index is used
ggplot(ts_df, aes(y='value')) + geom_line()
# Auto-populate: if x is omitted, index is used
ggplot(ts_df, aes(y='value')) + geom_line()
Out[4]:
Color and Fill¶
color affects lines and point outlines. fill affects filled areas.
In [5]:
Copied!
# Points: color = outline
ggplot(df, aes(x='x', y='y', color='species')) + geom_point(size=5)
# Points: color = outline
ggplot(df, aes(x='x', y='y', color='species')) + geom_point(size=5)
Out[5]:
In [6]:
Copied!
# Bars: fill = bar color
ggplot(bar_df, aes(x='category', y='count', fill='category')) + geom_bar(stat='identity')
# Bars: fill = bar color
ggplot(bar_df, aes(x='category', y='count', fill='category')) + geom_bar(stat='identity')
Out[6]:
Grouping¶
The group aesthetic controls how data is grouped for geoms like geom_line:
In [7]:
Copied!
# With group: separate line per group
ggplot(line_df, aes(x='x', y='y', group='id')) + geom_line()
# With group: separate line per group
ggplot(line_df, aes(x='x', y='y', group='id')) + geom_line()
Out[7]:
In [8]:
Copied!
# color automatically sets group
ggplot(line_df, aes(x='x', y='y', color='id')) + geom_line()
# color automatically sets group
ggplot(line_df, aes(x='x', y='y', color='id')) + geom_line()
Out[8]:
Inheriting Aesthetics¶
Aesthetics set in ggplot() are inherited by all geoms:
In [9]:
Copied!
# Both geoms use x='x', y='y', color='species'
(
ggplot(line_df, aes(x='x', y='y', color='id'))
+ geom_point()
+ geom_line()
)
# Both geoms use x='x', y='y', color='species'
(
ggplot(line_df, aes(x='x', y='y', color='id'))
+ geom_point()
+ geom_line()
)
Out[9]:
Override in individual geoms:
In [10]:
Copied!
(
ggplot(df, aes(x='x', y='y'))
+ geom_point(aes(color='species')) # Points colored by species
+ geom_smooth(color='gray') # Smooth line is gray
)
(
ggplot(df, aes(x='x', y='y'))
+ geom_point(aes(color='species')) # Points colored by species
+ geom_smooth(color='gray') # Smooth line is gray
)
Out[10]:
In [11]:
Copied!
# Named colors
ggplot(df, aes(x='x', y='y')) + geom_point(color='steelblue', size=5)
# Named colors
ggplot(df, aes(x='x', y='y')) + geom_point(color='steelblue', size=5)
Out[11]:
In [12]:
Copied!
# Hex colors
ggplot(df, aes(x='x', y='y')) + geom_point(color='#FF5733', size=5)
# Hex colors
ggplot(df, aes(x='x', y='y')) + geom_point(color='#FF5733', size=5)
Out[12]:
Shapes¶
In [13]:
Copied!
# Plotly marker symbols
ggplot(df, aes(x='x', y='y')) + geom_point(shape='diamond', size=8)
# Plotly marker symbols
ggplot(df, aes(x='x', y='y')) + geom_point(shape='diamond', size=8)
Out[13]:
In [14]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_point(shape='triangle-up', size=8)
ggplot(df, aes(x='x', y='y')) + geom_point(shape='triangle-up', size=8)
Out[14]:
Line Types¶
In [15]:
Copied!
ggplot(ts_df, aes(y='value')) + geom_line(linetype='dash')
ggplot(ts_df, aes(y='value')) + geom_line(linetype='dash')
Out[15]:
In [16]:
Copied!
ggplot(ts_df, aes(y='value')) + geom_line(linetype='dot')
ggplot(ts_df, aes(y='value')) + geom_line(linetype='dot')
Out[16]: