Chart¶
The main class for creating and configuring charts.
Main class for creating and configuring charts.
Provides a fluent API for mapping data columns to chart attributes, adding geometric layers, faceting, theming, displaying, and exporting charts. Supports pandas DataFrame, polars DataFrame, and numpy ndarray as input.
Example
__init__(data=None, map=None)
¶
Initialize a Chart with optional data and column mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame | DataFrame | ndarray | None
|
Input data as a pandas DataFrame, polars DataFrame, or numpy ndarray. |
None
|
map
|
dict | None
|
Dictionary mapping chart attributes to data column names
(e.g., |
None
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If the provided data is not a supported type. |
map(**kwargs)
¶
Map data columns to chart visual attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
str
|
Chart attribute to column name mappings.
Common keys: |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Chart |
The chart instance for method chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the mapping is invalid or columns don't exist in the data. |
title(title, subtitle=None)
¶
Set the chart title and optional subtitle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
The main title text. |
required |
subtitle
|
str | None
|
Optional subtitle text displayed below the title. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Chart |
The chart instance for method chaining. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If title or subtitle is not a string. |
geom(type, **kwargs)
¶
Add a geometric layer to the chart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type
|
str
|
Chart type — |
required |
**kwargs
|
Optional overrides:
|
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Chart |
The chart instance for method chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the chart type or mapping is invalid. |
axis(axis=None, type=None, name=None, n_breaks=5, breaks=None, minor_breaks=None, breaks_labels=None, limits=None, expand=[0.05, 0], inverse=False)
¶
Configure an axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axis
|
str
|
Which axis to configure — |
None
|
type
|
str | None
|
Axis scale type —
|
None
|
name
|
str | None
|
Axis label text. |
None
|
n_breaks
|
int
|
Target number of major tick marks. |
5
|
breaks
|
list | None
|
Explicit positions for major ticks. |
None
|
minor_breaks
|
list | None
|
Explicit positions for minor ticks. |
None
|
breaks_labels
|
list | None
|
Custom labels for major ticks. |
None
|
limits
|
list | None
|
Axis range as |
None
|
expand
|
list
|
Expansion factor as |
[0.05, 0]
|
inverse
|
bool
|
Whether to invert the axis direction. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Chart |
The chart instance for method chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If axis is not |
matrix(col=None, row=None)
¶
Configure a matrix grid layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col
|
list[str] | None
|
Column names to display in the matrix. |
None
|
row
|
list[str] | None
|
Row names to display in the matrix. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Chart |
The chart instance for method chaining. |
facet(col=None, row=None, scales='fixed', space=0.1)
¶
Split the chart into a grid of subplots by data variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col
|
str | None
|
Column name to facet by (creates columns of subplots). |
None
|
row
|
str | None
|
Column name to facet by (creates rows of subplots). |
None
|
scales
|
str
|
How axes are shared across facets —
|
'fixed'
|
space
|
float
|
Relative spacing between facets, between 0 and 1. |
0.1
|
Returns:
| Name | Type | Description |
|---|---|---|
Chart |
The chart instance for method chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither |
theme(theme=None)
¶
Apply a theme to the chart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theme
|
Theme
|
A |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Chart |
The chart instance for method chaining. |
display(width='600px', height='400px')
¶
Display the chart in a Jupyter notebook.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
str
|
Chart width as a CSS value (e.g., |
'600px'
|
height
|
str
|
Chart height as a CSS value (e.g., |
'400px'
|
print(width='600px', height='400px')
¶
Return the chart configuration as a JSON text string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
str
|
Chart width as a CSS value (e.g., |
'600px'
|
height
|
str
|
Chart height as a CSS value (e.g., |
'400px'
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
JSON string of the chart options. |
save(file_name, type='html', width='600px', height='400px')
¶
Save the chart to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
Output file path. |
required |
type
|
str
|
Export format — |
'html'
|
width
|
str
|
Chart width as a CSS value (e.g., |
'600px'
|
height
|
str
|
Chart height as a CSS value (e.g., |
'400px'
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
JSON string of chart options when |
Raises:
| Type | Description |
|---|---|
ValueError
|
If type is not |