Geoms¶
Geoms (geometric objects) are the visual elements that represent your data. ggplotly provides 46 geoms for different visualization types.
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
})
# 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,
'series': np.tile(['A', 'B'], 50)
})
# Bar data
bar_df = pd.DataFrame({
'category': ['A', 'B', 'C', 'D'],
'count': [25, 40, 35, 30]
})
# Distribution data
dist_df = pd.DataFrame({
'category': np.repeat(['A', 'B', 'C'], 50),
'value': np.concatenate([
np.random.normal(0, 1, 50),
np.random.normal(2, 1.5, 50),
np.random.normal(-1, 0.8, 50)
])
})
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
})
# 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,
'series': np.tile(['A', 'B'], 50)
})
# Bar data
bar_df = pd.DataFrame({
'category': ['A', 'B', 'C', 'D'],
'count': [25, 40, 35, 30]
})
# Distribution data
dist_df = pd.DataFrame({
'category': np.repeat(['A', 'B', 'C'], 50),
'value': np.concatenate([
np.random.normal(0, 1, 50),
np.random.normal(2, 1.5, 50),
np.random.normal(-1, 0.8, 50)
])
})
In [2]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_point()
ggplot(df, aes(x='x', y='y')) + geom_point()
Out[2]:
In [3]:
Copied!
# With aesthetics
ggplot(df, aes(x='x', y='y', color='category', size='value')) + geom_point()
# With aesthetics
ggplot(df, aes(x='x', y='y', color='category', size='value')) + geom_point()
Out[3]:
In [4]:
Copied!
# Parameters
ggplot(df, aes(x='x', y='y')) + geom_point(size=5, alpha=0.7, shape='diamond')
# Parameters
ggplot(df, aes(x='x', y='y')) + geom_point(size=5, alpha=0.7, shape='diamond')
Out[4]:
geom_line¶
Line plots connecting points in order of x values.
In [5]:
Copied!
ggplot(ts_df, aes(x='date', y='value')) + geom_line()
ggplot(ts_df, aes(x='date', y='value')) + geom_line()
Out[5]:
In [6]:
Copied!
# Multiple lines
ggplot(ts_df, aes(x='date', y='value', color='series')) + geom_line()
# Multiple lines
ggplot(ts_df, aes(x='date', y='value', color='series')) + geom_line()
Out[6]:
geom_path¶
Like geom_line, but connects points in data order (not sorted by x).
In [7]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_path()
ggplot(df, aes(x='x', y='y')) + geom_path()
Out[7]:
geom_bar¶
Bar charts for categorical data.
In [8]:
Copied!
# Count occurrences
ggplot(df, aes(x='category')) + geom_bar()
# Count occurrences
ggplot(df, aes(x='category')) + geom_bar()
Out[8]:
In [9]:
Copied!
# Pre-computed heights
ggplot(bar_df, aes(x='category', y='count')) + geom_bar(stat='identity')
# Pre-computed heights
ggplot(bar_df, aes(x='category', y='count')) + geom_bar(stat='identity')
Out[9]:
geom_col¶
Alias for geom_bar(stat='identity').
In [10]:
Copied!
ggplot(bar_df, aes(x='category', y='count')) + geom_col()
ggplot(bar_df, aes(x='category', y='count')) + geom_col()
Out[10]:
In [11]:
Copied!
ggplot(df, aes(x='value')) + geom_histogram()
ggplot(df, aes(x='value')) + geom_histogram()
Out[11]:
In [12]:
Copied!
# Control bins
ggplot(df, aes(x='value')) + geom_histogram(bins=30)
# Control bins
ggplot(df, aes(x='value')) + geom_histogram(bins=30)
Out[12]:
geom_density¶
Kernel density estimation.
In [13]:
Copied!
ggplot(dist_df, aes(x='value')) + geom_density()
ggplot(dist_df, aes(x='value')) + geom_density()
Out[13]:
In [14]:
Copied!
# Filled density
ggplot(dist_df, aes(x='value', fill='category')) + geom_density(alpha=0.5)
# Filled density
ggplot(dist_df, aes(x='value', fill='category')) + geom_density(alpha=0.5)
Out[14]:
geom_boxplot¶
Box-and-whisker plots.
In [15]:
Copied!
ggplot(dist_df, aes(x='category', y='value')) + geom_boxplot()
ggplot(dist_df, aes(x='category', y='value')) + geom_boxplot()
Out[15]:
geom_violin¶
Violin plots showing distribution shape.
In [16]:
Copied!
ggplot(dist_df, aes(x='category', y='value')) + geom_violin()
ggplot(dist_df, aes(x='category', y='value')) + geom_violin()
Out[16]:
In [17]:
Copied!
ggplot(ts_df[ts_df['series'] == 'A'], aes(x='date', y='value')) + geom_area()
ggplot(ts_df[ts_df['series'] == 'A'], aes(x='date', y='value')) + geom_area()
Out[17]:
geom_ribbon¶
Area between ymin and ymax.
In [18]:
Copied!
# Create data with error bounds
ribbon_df = ts_df[ts_df['series'] == 'A'].copy()
ribbon_df['lower'] = ribbon_df['value'] - 5
ribbon_df['upper'] = ribbon_df['value'] + 5
ggplot(ribbon_df, aes(x='date', ymin='lower', ymax='upper')) + geom_ribbon(alpha=0.3)
# Create data with error bounds
ribbon_df = ts_df[ts_df['series'] == 'A'].copy()
ribbon_df['lower'] = ribbon_df['value'] - 5
ribbon_df['upper'] = ribbon_df['value'] + 5
ggplot(ribbon_df, aes(x='date', ymin='lower', ymax='upper')) + geom_ribbon(alpha=0.3)
Out[18]:
geom_rect¶
Rectangles defined by xmin, xmax, ymin, ymax.
In [19]:
Copied!
# Rectangle data
rect_df = pd.DataFrame({
'xmin': [0, 2, 4],
'xmax': [1, 3, 5],
'ymin': [0, 1, 0.5],
'ymax': [2, 3, 2.5],
'category': ['A', 'B', 'C']
})
ggplot(rect_df, aes(xmin='xmin', xmax='xmax', ymin='ymin', ymax='ymax', fill='category')) + geom_rect(alpha=0.7)
# Rectangle data
rect_df = pd.DataFrame({
'xmin': [0, 2, 4],
'xmax': [1, 3, 5],
'ymin': [0, 1, 0.5],
'ymax': [2, 3, 2.5],
'category': ['A', 'B', 'C']
})
ggplot(rect_df, aes(xmin='xmin', xmax='xmax', ymin='ymin', ymax='ymax', fill='category')) + geom_rect(alpha=0.7)
Out[19]:
In [20]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_point() + geom_smooth()
ggplot(df, aes(x='x', y='y')) + geom_point() + geom_smooth()
Out[20]:
In [21]:
Copied!
# Linear regression
ggplot(df, aes(x='x', y='y')) + geom_point() + geom_smooth(method='lm')
# Linear regression
ggplot(df, aes(x='x', y='y')) + geom_point() + geom_smooth(method='lm')
Out[21]:
In [22]:
Copied!
ggplot(bar_df, aes(x='category', y='count', label='count')) + geom_col() + geom_text()
ggplot(bar_df, aes(x='category', y='count', label='count')) + geom_col() + geom_text()
Out[22]:
geom_label¶
Text labels with a background box.
In [23]:
Copied!
ggplot(bar_df, aes(x='category', y='count', label='count')) + geom_col() + geom_label()
ggplot(bar_df, aes(x='category', y='count', label='count')) + geom_col() + geom_label()
Out[23]:
In [24]:
Copied!
# Horizontal line
ggplot(df, aes(x='x', y='y')) + geom_point() + geom_hline(yintercept=0)
# Horizontal line
ggplot(df, aes(x='x', y='y')) + geom_point() + geom_hline(yintercept=0)
Out[24]:
In [25]:
Copied!
# Vertical line
ggplot(df, aes(x='x', y='y')) + geom_point() + geom_vline(xintercept=0)
# Vertical line
ggplot(df, aes(x='x', y='y')) + geom_point() + geom_vline(xintercept=0)
Out[25]:
In [26]:
Copied!
# Diagonal line
ggplot(df, aes(x='x', y='y')) + geom_point() + geom_abline(slope=1, intercept=0)
# Diagonal line
ggplot(df, aes(x='x', y='y')) + geom_point() + geom_abline(slope=1, intercept=0)
Out[26]:
In [27]:
Copied!
# Heatmap data
x_grid = np.arange(10)
y_grid = np.arange(10)
X, Y = np.meshgrid(x_grid, y_grid)
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()
# Heatmap data
x_grid = np.arange(10)
y_grid = np.arange(10)
X, Y = np.meshgrid(x_grid, y_grid)
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()
Out[27]:
geom_contour / geom_contour_filled¶
Contour plots for 3D data on 2D.
In [28]:
Copied!
ggplot(tile_df, aes(x='x', y='y', z='z')) + geom_contour()
ggplot(tile_df, aes(x='x', y='y', z='z')) + geom_contour()
Out[28]:
In [29]:
Copied!
ggplot(tile_df, aes(x='x', y='y', z='z')) + geom_contour_filled()
ggplot(tile_df, aes(x='x', y='y', z='z')) + geom_contour_filled()
Out[29]:
geom_segment¶
Line segments between two points.
In [30]:
Copied!
# Segment data
seg_df = pd.DataFrame({
'x': [0, 1, 2],
'y': [0, 1, 0],
'xend': [1, 2, 3],
'yend': [1, 0, 1]
})
ggplot(seg_df, aes(x='x', y='y', xend='xend', yend='yend')) + geom_segment()
# Segment data
seg_df = pd.DataFrame({
'x': [0, 1, 2],
'y': [0, 1, 0],
'xend': [1, 2, 3],
'yend': [1, 0, 1]
})
ggplot(seg_df, aes(x='x', y='y', xend='xend', yend='yend')) + geom_segment()
Out[30]:
geom_errorbar¶
Error bars.
In [31]:
Copied!
# Error bar data
err_df = pd.DataFrame({
'x': ['A', 'B', 'C'],
'y': [10, 15, 12],
'ymin': [8, 12, 10],
'ymax': [12, 18, 14]
})
ggplot(err_df, aes(x='x', y='y', ymin='ymin', ymax='ymax')) + geom_col() + geom_errorbar()
# Error bar data
err_df = pd.DataFrame({
'x': ['A', 'B', 'C'],
'y': [10, 15, 12],
'ymin': [8, 12, 10],
'ymax': [12, 18, 14]
})
ggplot(err_df, aes(x='x', y='y', ymin='ymin', ymax='ymax')) + geom_col() + geom_errorbar()
Out[31]:
In [32]:
Copied!
# 3D data
df_3d = pd.DataFrame({
'x': np.random.randn(50),
'y': np.random.randn(50),
'z': np.random.randn(50)
})
ggplot(df_3d, aes(x='x', y='y', z='z')) + geom_point_3d()
# 3D data
df_3d = pd.DataFrame({
'x': np.random.randn(50),
'y': np.random.randn(50),
'z': np.random.randn(50)
})
ggplot(df_3d, aes(x='x', y='y', z='z')) + geom_point_3d()
Out[32]:
geom_surface¶
3D surface plots.
In [33]:
Copied!
ggplot(tile_df, aes(x='x', y='y', z='z')) + geom_surface()
ggplot(tile_df, aes(x='x', y='y', z='z')) + geom_surface()
Out[33]:
Complete Geom List¶
| Geom | Description |
|---|---|
geom_point |
Scatter plots |
geom_line |
Line plots (sorted by x) |
geom_lines |
Multi-series line plots |
geom_path |
Path plots (data order) |
geom_bar |
Bar charts |
geom_col |
Column charts |
geom_histogram |
Histograms |
geom_boxplot |
Box plots |
geom_violin |
Violin plots |
geom_density |
Density plots |
geom_area |
Area plots |
geom_ribbon |
Ribbon plots |
geom_rect |
Rectangles |
geom_smooth |
Smoothed lines |
geom_tile |
Heatmaps |
geom_text |
Text labels |
geom_label |
Text labels with background |
geom_errorbar |
Error bars |
geom_segment |
Line segments |
geom_step |
Step plots |
geom_rug |
Rug plots |
geom_jitter |
Jittered points |
geom_vline |
Vertical lines |
geom_hline |
Horizontal lines |
geom_abline |
Diagonal lines |
geom_contour |
Contour lines |
geom_contour_filled |
Filled contours |
geom_map |
Choropleth maps |
geom_sf |
Simple features |
geom_range |
Range plots |
geom_edgebundle |
Edge bundling |
geom_searoute |
Sea routes |
geom_fanchart |
Fan charts for uncertainty |
geom_point_3d |
3D points |
geom_surface |
3D surfaces |
geom_wireframe |
3D wireframes |
geom_candlestick |
Candlestick charts |
geom_ohlc |
OHLC charts |
geom_waterfall |
Waterfall charts |
geom_sankey |
Sankey flow diagrams |
geom_stl |
STL decomposition plots |
geom_acf |
Autocorrelation plots |
geom_pacf |
Partial autocorrelation plots |
geom_norm |
Normal distribution overlay |
geom_qq |
Q-Q plots |
geom_qq_line |
Q-Q reference line |