Skip to content

Add extra themes #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ jobs:
3d_charts,
basic_charts,
custom_controls,
customization,
financial_charts,
images,
kaleido,
Expand All @@ -103,6 +104,7 @@ jobs:
scientific_charts,
shapes,
subplots,
themes,
]
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -140,5 +142,6 @@ jobs:
cd ${{ github.workspace }}/examples/3d_charts && cargo run
cd ${{ github.workspace }}/examples/subplots && cargo run
cd ${{ github.workspace }}/examples/shapes && cargo run
cd ${{ github.workspace }}/examples/themes && cargo run
- name: Build book
run: mdbook build docs/book
1 change: 1 addition & 0 deletions docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@
- [Subplots](./recipes/subplots.md)
- [Subplots](./recipes/subplots/subplots.md)
- [Multiple Axes](./recipes/subplots/multiple_axes.md)
- [Themes](./recipes/themes.md)
66 changes: 66 additions & 0 deletions docs/book/src/recipes/themes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Themes

The complete source code for the following examples can also be found [here](https://github.com/plotly/plotly.rs/tree/main/examples/themes).

# Use different theme templates

Similar to [Plotly Python templates](https://plotly.com/python/templates/), plotly.rs provides several built-in themes that can be applied to your plots.

```rust,no_run
use plotly::{
common::{Marker, Mode, Title},
layout::{Layout, BuiltinTheme},
Plot, Scatter,
};
```

The `to_inline_html` method is used to produce the html plot displayed in this page.

## Default Theme (Plotly)

{{#include ../../../../examples/themes/output/inline_gapminder_plotly.html}}

## Plotly White Theme

{{#include ../../../../examples/themes/output/inline_gapminder_plotly_white.html}}

## Plotly Dark Theme

{{#include ../../../../examples/themes/output/inline_gapminder_plotly_dark.html}}

## Seaborn Theme

{{#include ../../../../examples/themes/output/inline_gapminder_seaborn.html}}

## Matplotlib Theme

{{#include ../../../../examples/themes/output/inline_gapminder_matplotlib.html}}

## Plotnine Theme

{{#include ../../../../examples/themes/output/inline_gapminder_plotnine.html}}

## Available Themes

The following built-in themes are available in plotly.rs:

- `BuiltinTheme::Default` - Default Plotly theme
- `BuiltinTheme::PlotlyWhite` - Clean white background theme
- `BuiltinTheme::PlotlyDark` - Dark theme
- `BuiltinTheme::Seaborn` - Seaborn-style theme
- `BuiltinTheme::SeabornWhitegrid` - Seaborn with white grid
- `BuiltinTheme::SeabornDark` - Dark Seaborn theme
- `BuiltinTheme::Matplotlib` - Matplotlib-style theme
- `BuiltinTheme::Plotnine` - Plotnine-style theme

## Using Themes

To apply a theme to your plot, use the `template()` method on the layout:

```rust
let theme = BuiltinTheme::Seaborn;
let layout = Layout::new().template(theme.build());
plot.set_layout(layout);
```

The example above uses real Gapminder 2007 data showing the relationship between GDP per capita, life expectancy, and population size across different continents, with marker sizes representing population and colors representing continents.
1 change: 1 addition & 0 deletions examples/3d_charts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ edition = "2021"
ndarray = "0.16"
rand = "0.9"
plotly = { path = "../../plotly" }
plotly_utils = { path = "../plotly_utils" }
13 changes: 1 addition & 12 deletions examples/3d_charts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use plotly::{
layout::{Axis, Camera, Layout, LayoutScene, Legend, Margin, ProjectionType},
Mesh3D, Plot, Scatter3D, Surface,
};
use plotly_utils::write_example_to_html;
use rand::Rng;

// 3D Scatter Plots
Expand Down Expand Up @@ -259,18 +260,6 @@ fn colorscale_plot(show: bool, file_name: &str) {
}
// ANCHOR_END: colorscale_plot

fn write_example_to_html(plot: &Plot, name: &str) -> String {
std::fs::create_dir_all("./output").unwrap();
// Write inline HTML
let html = plot.to_inline_html(Some(name));
let path = format!("./output/inline_{}.html", name);
std::fs::write(path, html).unwrap();
// Write standalone HTML
let path = format!("./output/{}.html", name);
plot.write_html(&path);
path
}

fn main() {
// Change false to true on any of these lines to display the example.
// Scatter3D Plots
Expand Down
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[workspace]
members = ["*"]
members = ["*", "plotly_utils"]
resolver = "2"
exclude = ["jupyter", "target", "wasm-yew"]
1 change: 1 addition & 0 deletions examples/basic_charts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ edition = "2021"
[dependencies]
ndarray = "0.16"
plotly = { path = "../../plotly" }
plotly_utils = { path = "../plotly_utils" }
rand = "0.9"
rand_distr = "0.5"
13 changes: 1 addition & 12 deletions examples/basic_charts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use plotly::{
traces::table::{Cells, Header},
Bar, Pie, Plot, Sankey, Scatter, ScatterPolar, Table,
};
use plotly_utils::write_example_to_html;
use rand_distr::{Distribution, Normal, Uniform};

// Scatter Plots
Expand Down Expand Up @@ -996,18 +997,6 @@ fn grouped_donout_pie_charts(show: bool, file_name: &str) {
}
// ANCHOR_END: grouped_donout_pie_charts

fn write_example_to_html(plot: &Plot, name: &str) -> String {
std::fs::create_dir_all("./output").unwrap();
// Write inline HTML
let html = plot.to_inline_html(Some(name));
let path = format!("./output/inline_{}.html", name);
std::fs::write(path, html).unwrap();
// Write standalone HTML
let path = format!("./output/{}.html", name);
plot.write_html(&path);
path
}

fn main() {
// Change false to true on any of these lines to display the example.

Expand Down
1 change: 1 addition & 0 deletions examples/custom_controls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ edition = "2021"
[dependencies]
itertools = "0.10"
plotly = { path = "../../plotly" }
plotly_utils = { path = "../plotly_utils" }
13 changes: 1 addition & 12 deletions examples/custom_controls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use plotly::{
},
Bar, HeatMap, Layout, Plot,
};
use plotly_utils::write_example_to_html;

/// Display a bar chart with an associated dropdown selector to show different
/// data.
Expand Down Expand Up @@ -117,18 +118,6 @@ fn bar_chart_with_modifiable_bar_mode(show: bool, file_name: &str) {
}
// ANCHOR_END: colorscale_plot

fn write_example_to_html(plot: &Plot, name: &str) -> String {
std::fs::create_dir_all("./output").unwrap();
// Write inline HTML
let html = plot.to_inline_html(Some(name));
let path = format!("./output/inline_{}.html", name);
std::fs::write(path, html).unwrap();
// Write standalone HTML
let path = format!("./output/{}.html", name);
plot.write_html(&path);
path
}

fn main() {
// Change false to true on any of these lines to display the example.

Expand Down
1 change: 1 addition & 0 deletions examples/customization/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ build_html = "2.5.0"
rand = "0.9"
ndarray = "0.16"
plotly = { path = "../../plotly" }
plotly_utils = { path = "../plotly_utils" }
13 changes: 1 addition & 12 deletions examples/customization/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use plotly::{
layout::{Center, DragMode, Mapbox, MapboxStyle, Margin},
Configuration, DensityMapbox, Layout, Plot, Scatter, Scatter3D,
};
use plotly_utils::write_example_to_html;
const DEFAULT_HTML_APP_NOT_FOUND: &str = "Could not find default application for HTML files.";

fn density_mapbox_responsive_autofill(show: bool, file_name: &str) {
Expand Down Expand Up @@ -130,18 +131,6 @@ fn show_with_default_app(temp_path: &str) {
.expect(DEFAULT_HTML_APP_NOT_FOUND);
}

fn write_example_to_html(plot: &Plot, name: &str) -> String {
std::fs::create_dir_all("./output").unwrap();
// Write inline HTML
let html = plot.to_inline_html(Some(name));
let path = format!("./output/inline_{}.html", name);
std::fs::write(path, html).unwrap();
// Write standalone HTML
let path = format!("./output/{}.html", name);
plot.write_html(&path);
path
}

fn main() {
// Switch the boolean flag to `true` to display the example, otherwise manually
// open the generated file in the `output` folder.
Expand Down
1 change: 1 addition & 0 deletions examples/financial_charts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ edition = "2021"
[dependencies]
csv = "1.1"
plotly = { path = "../../plotly" }
plotly_utils = { path = "../plotly_utils" }
serde = "1.0"
13 changes: 1 addition & 12 deletions examples/financial_charts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::path::PathBuf;
use plotly::common::TickFormatStop;
use plotly::layout::{Axis, RangeSelector, RangeSlider, SelectorButton, SelectorStep, StepMode};
use plotly::{Candlestick, Layout, Ohlc, Plot, Scatter};
use plotly_utils::write_example_to_html;
use serde::Deserialize;

#[derive(Deserialize)]
Expand Down Expand Up @@ -319,18 +320,6 @@ fn simple_ohlc_chart(show: bool, file_name: &str) {
}
// ANCHOR_END: simple_ohlc_chart

fn write_example_to_html(plot: &Plot, name: &str) -> String {
std::fs::create_dir_all("./output").unwrap();
// Write inline HTML
let html = plot.to_inline_html(Some(name));
let path = format!("./output/inline_{}.html", name);
std::fs::write(path, html).unwrap();
// Write standalone HTML
let path = format!("./output/{}.html", name);
plot.write_html(&path);
path
}

fn main() {
// Change false to true on any of these lines to display the example.

Expand Down
2 changes: 2 additions & 0 deletions examples/images/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ plotly = { path = "../../plotly", features = [
"plotly_image",
"plotly_ndarray",
] }

plotly_utils = { path = "../plotly_utils" }
13 changes: 1 addition & 12 deletions examples/images/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use ndarray::arr2;
use plotly::{color::Rgb, image::ColorModel, Image, Plot};
use plotly_utils::write_example_to_html;

fn basic_image(show: bool, file_name: &str) {
let w = Rgb::new(255, 255, 255);
Expand Down Expand Up @@ -102,18 +103,6 @@ fn trace_from_ndarray_rgba(show: bool, file_name: &str) {
}
}

fn write_example_to_html(plot: &Plot, name: &str) -> String {
std::fs::create_dir_all("./output").unwrap();
// Write inline HTML
let html = plot.to_inline_html(Some(name));
let path = format!("./output/inline_{}.html", name);
std::fs::write(path, html).unwrap();
// Write standalone HTML
let path = format!("./output/{}.html", name);
plot.write_html(&path);
path
}

fn main() {
// Change false to true on any of these lines to display the example.
basic_image(false, "basic_image");
Expand Down
1 change: 1 addition & 0 deletions examples/maps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"

[dependencies]
plotly = { path = "../../plotly" }
plotly_utils = { path = "../plotly_utils" }
csv = "1.3"
reqwest = { version = "0.11", features = ["blocking"] }

13 changes: 1 addition & 12 deletions examples/maps/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use plotly::{
},
DensityMapbox, Layout, Plot, ScatterGeo, ScatterMapbox,
};
use plotly_utils::write_example_to_html;

fn scatter_mapbox(show: bool, file_name: &str) {
let trace = ScatterMapbox::new(vec![45.5017], vec![-73.5673])
Expand Down Expand Up @@ -159,18 +160,6 @@ fn density_mapbox(show: bool, file_name: &str) {
}
}

fn write_example_to_html(plot: &Plot, name: &str) -> String {
std::fs::create_dir_all("./output").unwrap();
// Write inline HTML
let html = plot.to_inline_html(Some(name));
let path = format!("./output/inline_{}.html", name);
std::fs::write(path, html).unwrap();
// Write standalone HTML
let path = format!("./output/{}.html", name);
plot.write_html(&path);
path
}

fn main() {
// Change false to true on any of these lines to display the example.
scatter_mapbox(false, "scatter_mapbox");
Expand Down
1 change: 1 addition & 0 deletions examples/ndarray/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ edition = "2021"
[dependencies]
ndarray = "0.16"
plotly = { path = "../../plotly", features = ["plotly_ndarray"] }
plotly_utils = { path = "../plotly_utils" }
13 changes: 1 addition & 12 deletions examples/ndarray/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ndarray::{Array, Ix1, Ix2};
use plotly::common::Mode;
use plotly::ndarray::ArrayTraces;
use plotly::{Plot, Scatter};
use plotly_utils::write_example_to_html;

fn single_ndarray_trace(show: bool, file_name: &str) {
let n: usize = 11;
Expand Down Expand Up @@ -73,18 +74,6 @@ fn multiple_ndarray_traces_over_rows(show: bool, file_name: &str) {
}
}

fn write_example_to_html(plot: &Plot, name: &str) -> String {
std::fs::create_dir_all("./output").unwrap();
// Write inline HTML
let html = plot.to_inline_html(Some(name));
let path = format!("./output/inline_{}.html", name);
std::fs::write(path, html).unwrap();
// Write standalone HTML
let path = format!("./output/{}.html", name);
plot.write_html(&path);
path
}

fn main() {
// Change false to true on any of these lines to display the example.
single_ndarray_trace(false, "single_ndarray_trace");
Expand Down
7 changes: 7 additions & 0 deletions examples/plotly_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "plotly_utils"
version = "0.1.0"
edition = "2021"

[dependencies]
plotly = { path = "../../plotly" }
Loading
Loading