Skip to content

Commit 30b0367

Browse files
committed
adding extra themes
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 0f00d10 commit 30b0367

File tree

4 files changed

+583
-2
lines changed

4 files changed

+583
-2
lines changed

examples/themes/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "themes"
3+
version = "0.1.0"
4+
authors = ["Andrei Gherghescu [email protected]"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
ndarray = "0.16"
9+
csv = "1.1"
10+
plotly = { path = "../../plotly" }
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
country,pop,continent,lifeExp,gdpPercap
2+
Afghanistan,31889923.0,Asia,43.828,974.5803384
3+
Albania,3600523.0,Europe,76.423,5937.029525999999
4+
Algeria,33333216.0,Africa,72.301,6223.367465
5+
Angola,12420476.0,Africa,42.731,4797.231267
6+
Argentina,40301927.0,Americas,75.32,12779.379640000001
7+
Australia,20434176.0,Oceania,81.235,34435.367439999995
8+
Austria,8199783.0,Europe,79.829,36126.4927
9+
Bahrain,708573.0,Asia,75.635,29796.048339999998
10+
Bangladesh,150448339.0,Asia,64.062,1391.253792
11+
Belgium,10392226.0,Europe,79.441,33692.60508
12+
Benin,8078314.0,Africa,56.728,1441.284873
13+
Bolivia,9119152.0,Americas,65.554,3822.1370840000004
14+
Bosnia and Herzegovina,4552198.0,Europe,74.852,7446.298803
15+
Botswana,1639131.0,Africa,50.728,12569.851770000001
16+
Brazil,190010647.0,Americas,72.39,9065.800825
17+
Bulgaria,7322858.0,Europe,73.005,10680.79282
18+
Burkina Faso,14326203.0,Africa,52.295,1217.032994
19+
Burundi,8390505.0,Africa,49.58,430.07069160000003
20+
Cambodia,14131858.0,Asia,59.723,1713.7786859999999
21+
Cameroon,17696293.0,Africa,50.43,2042.0952399999999
22+
Canada,33390141.0,Americas,80.653,36319.235010000004
23+
Central African Republic,4369038.0,Africa,44.74100000000001,706.016537
24+
Chad,10238807.0,Africa,50.651,1704.0637239999999
25+
Chile,16284741.0,Americas,78.553,13171.63885
26+
China,1318683096.0,Asia,72.961,4959.1148539999995
27+
Colombia,44227550.0,Americas,72.889,7006.580419
28+
Comoros,710960.0,Africa,65.152,986.1478792000001
29+
"Congo, Dem. Rep.",64606759.0,Africa,46.461999999999996,277.55185869999997
30+
"Congo, Rep.",3800610.0,Africa,55.321999999999996,3632.557798
31+
Costa Rica,4133884.0,Americas,78.782,9645.06142
32+
Cote d'Ivoire,18013409.0,Africa,48.328,1544.750112
33+
Croatia,4493312.0,Europe,75.748,14619.222719999998
34+
Cuba,11416987.0,Americas,78.273,8948.102923
35+
Czech Republic,10228744.0,Europe,76.486,22833.30851
36+
Denmark,5468120.0,Europe,78.332,35278.41874
37+
Djibouti,496374.0,Africa,54.791000000000004,2082.4815670000003
38+
Dominican Republic,9319622.0,Americas,72.235,6025.374752000001
39+
Ecuador,13755680.0,Americas,74.994,6873.262326000001
40+
Egypt,80264543.0,Africa,71.33800000000001,5581.180998
41+
El Salvador,6939688.0,Americas,71.878,5728.353514
42+
Equatorial Guinea,551201.0,Africa,51.57899999999999,12154.08975
43+
Eritrea,4906585.0,Africa,58.04,641.3695236000001
44+
Ethiopia,76511887.0,Africa,52.946999999999996,690.8055759
45+
Finland,5238460.0,Europe,79.313,33207.0844
46+
France,61083916.0,Europe,80.657,30470.0167
47+
Gabon,1454867.0,Africa,56.735,13206.48452
48+
Gambia,1688359.0,Africa,59.448,752.7497265
49+
Germany,82400996.0,Europe,79.406,32170.37442
50+
Ghana,22873338.0,Africa,60.022,1327.60891
51+
Greece,10706290.0,Europe,79.483,27538.41188
52+
Guatemala,12572928.0,Americas,70.259,5186.050003
53+
Guinea,9947814.0,Africa,56.007,942.6542111
54+
Guinea-Bissau,1472041.0,Africa,46.388000000000005,579.2317429999999
55+
Haiti,8502814.0,Americas,60.916000000000004,1201.637154
56+
Honduras,7483763.0,Americas,70.19800000000001,3548.3308460000003
57+
"Hong Kong, China",6980412.0,Asia,82.208,39724.97867
58+
Hungary,9956108.0,Europe,73.33800000000001,18008.94444
59+
Iceland,301931.0,Europe,81.757,36180.789189999996
60+
India,1110396331.0,Asia,64.69800000000001,2452.210407
61+
Indonesia,223547000.0,Asia,70.65,3540.6515640000002
62+
Iran,69453570.0,Asia,70.964,11605.71449
63+
Iraq,27499638.0,Asia,59.545,4471.061906
64+
Ireland,4109086.0,Europe,78.885,40675.99635
65+
Israel,6426679.0,Asia,80.745,25523.2771
66+
Italy,58147733.0,Europe,80.546,28569.7197
67+
Jamaica,2780132.0,Americas,72.567,7320.880262000001
68+
Japan,127467972.0,Asia,82.603,31656.06806
69+
Jordan,6053193.0,Asia,72.535,4519.461171
70+
Kenya,35610177.0,Africa,54.11,1463.249282
71+
"Korea, Dem. Rep.",23301725.0,Asia,67.297,1593.06548
72+
"Korea, Rep.",49044790.0,Asia,78.623,23348.139730000003
73+
Kuwait,2505559.0,Asia,77.58800000000001,47306.98978
74+
Lebanon,3921278.0,Asia,71.993,10461.05868
75+
Lesotho,2012649.0,Africa,42.592,1569.331442
76+
Liberia,3193942.0,Africa,45.678000000000004,414.5073415
77+
Libya,6036914.0,Africa,73.952,12057.49928
78+
Madagascar,19167654.0,Africa,59.443000000000005,1044.770126
79+
Malawi,13327079.0,Africa,48.303000000000004,759.3499101
80+
Malaysia,24821286.0,Asia,74.241,12451.6558
81+
Mali,12031795.0,Africa,54.467,1042.581557
82+
Mauritania,3270065.0,Africa,64.164,1803.1514960000002
83+
Mauritius,1250882.0,Africa,72.801,10956.99112
84+
Mexico,108700891.0,Americas,76.195,11977.57496
85+
Mongolia,2874127.0,Asia,66.803,3095.7722710000003
86+
Montenegro,684736.0,Europe,74.543,9253.896111
87+
Morocco,33757175.0,Africa,71.164,3820.17523
88+
Mozambique,19951656.0,Africa,42.082,823.6856205
89+
Myanmar,47761980.0,Asia,62.068999999999996,944.0
90+
Namibia,2055080.0,Africa,52.906000000000006,4811.060429
91+
Nepal,28901790.0,Asia,63.785,1091.359778
92+
Netherlands,16570613.0,Europe,79.762,36797.93332
93+
New Zealand,4115771.0,Oceania,80.204,25185.00911
94+
Nicaragua,5675356.0,Americas,72.899,2749.320965
95+
Niger,12894865.0,Africa,56.867,619.6768923999999
96+
Nigeria,135031164.0,Africa,46.858999999999995,2013.9773050000001
97+
Norway,4627926.0,Europe,80.196,49357.19017
98+
Oman,3204897.0,Asia,75.64,22316.19287
99+
Pakistan,169270617.0,Asia,65.483,2605.94758
100+
Panama,3242173.0,Americas,75.53699999999999,9809.185636
101+
Paraguay,6667147.0,Americas,71.752,4172.838464
102+
Peru,28674757.0,Americas,71.421,7408.905561
103+
Philippines,91077287.0,Asia,71.688,3190.481016
104+
Poland,38518241.0,Europe,75.563,15389.924680000002
105+
Portugal,10642836.0,Europe,78.098,20509.64777
106+
Puerto Rico,3942491.0,Americas,78.74600000000001,19328.70901
107+
Reunion,798094.0,Africa,76.442,7670.122558
108+
Romania,22276056.0,Europe,72.476,10808.47561
109+
Rwanda,8860588.0,Africa,46.242,863.0884639000001
110+
Sao Tome and Principe,199579.0,Africa,65.528,1598.435089
111+
Saudi Arabia,27601038.0,Asia,72.777,21654.83194
112+
Senegal,12267493.0,Africa,63.062,1712.4721359999999
113+
Serbia,10150265.0,Europe,74.002,9786.534714
114+
Sierra Leone,6144562.0,Africa,42.568000000000005,862.5407561000001
115+
Singapore,4553009.0,Asia,79.972,47143.179639999995
116+
Slovak Republic,5447502.0,Europe,74.663,18678.31435
117+
Slovenia,2009245.0,Europe,77.926,25768.25759
118+
Somalia,9118773.0,Africa,48.159,926.1410683
119+
South Africa,43997828.0,Africa,49.339,9269.657808
120+
Spain,40448191.0,Europe,80.941,28821.0637
121+
Sri Lanka,20378239.0,Asia,72.396,3970.0954070000003
122+
Sudan,42292929.0,Africa,58.556000000000004,2602.394995
123+
Swaziland,1133066.0,Africa,39.613,4513.480643
124+
Sweden,9031088.0,Europe,80.884,33859.74835
125+
Switzerland,7554661.0,Europe,81.70100000000001,37506.419069999996
126+
Syria,19314747.0,Asia,74.143,4184.548089
127+
Taiwan,23174294.0,Asia,78.4,28718.27684
128+
Tanzania,38139640.0,Africa,52.516999999999996,1107.482182
129+
Thailand,65068149.0,Asia,70.616,7458.3963269999995
130+
Togo,5701579.0,Africa,58.42,882.9699437999999
131+
Trinidad and Tobago,1056608.0,Americas,69.819,18008.50924
132+
Tunisia,10276158.0,Africa,73.923,7092.923025
133+
Turkey,71158647.0,Europe,71.777,8458.276384
134+
Uganda,29170398.0,Africa,51.542,1056.3801210000001
135+
United Kingdom,60776238.0,Europe,79.425,33203.26128
136+
United States,301139947.0,Americas,78.242,42951.65309
137+
Uruguay,3447496.0,Americas,76.384,10611.46299
138+
Venezuela,26084662.0,Americas,73.747,11415.805690000001
139+
Vietnam,85262356.0,Asia,74.249,2441.576404
140+
West Bank and Gaza,4018332.0,Asia,73.422,3025.349798
141+
"Yemen, Rep.",22211743.0,Asia,62.698,2280.769906
142+
Zambia,11746035.0,Africa,42.38399999999999,1271.211593
143+
Zimbabwe,12311143.0,Africa,43.486999999999995,469.70929810000007

examples/themes/src/main.rs

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#![allow(dead_code)]
2+
3+
use plotly::{
4+
common::{Marker, Mode, Title},
5+
layout::Layout,
6+
Plot, Scatter,
7+
};
8+
9+
use plotly::layout::themes::BuiltinTheme;
10+
11+
use std::fs::File;
12+
use std::io::BufReader;
13+
14+
use csv::ReaderBuilder;
15+
16+
// Read Gapminder 2007 data from CSV
17+
fn read_gapminder_data_from_csv() -> (Vec<f64>, Vec<f64>, Vec<f64>, Vec<String>, Vec<String>) {
18+
let file = File::open("assets/gapminder2007.csv").expect("Cannot open gapminder2007.csv");
19+
let mut rdr = ReaderBuilder::new().has_headers(true).from_reader(BufReader::new(file));
20+
let mut gdp_per_capita = Vec::new();
21+
let mut life_expectancy = Vec::new();
22+
let mut population = Vec::new();
23+
let mut continents = Vec::new();
24+
let mut countries = Vec::new();
25+
for result in rdr.records() {
26+
let record = result.expect("CSV record error");
27+
countries.push(record[0].to_string());
28+
population.push(record[1].parse::<f64>().unwrap_or(0.0));
29+
continents.push(record[2].to_string());
30+
life_expectancy.push(record[3].parse::<f64>().unwrap_or(0.0));
31+
gdp_per_capita.push(record[4].parse::<f64>().unwrap_or(0.0));
32+
}
33+
(gdp_per_capita, life_expectancy, population, continents, countries)
34+
}
35+
36+
fn create_gapminder_scatter_plot(theme: BuiltinTheme, show: bool) {
37+
let (gdp, life_exp, pop, continents, countries) = read_gapminder_data_from_csv();
38+
39+
let mut plot = Plot::new();
40+
let continent_colors = vec![
41+
("Asia", "#1f77b4"),
42+
("Europe", "#ff7f0e"),
43+
("Africa", "#2ca02c"),
44+
("Americas", "#d62728"),
45+
("Oceania", "#9467bd"),
46+
];
47+
48+
for (continent, color) in &continent_colors {
49+
let indices: Vec<usize> = continents
50+
.iter()
51+
.enumerate()
52+
.filter(|(_, c)| c == continent)
53+
.map(|(idx, _)| idx)
54+
.collect();
55+
if indices.is_empty() {
56+
continue;
57+
}
58+
let continent_gdp: Vec<f64> = indices.iter().map(|&idx| gdp[idx]).collect();
59+
let continent_life: Vec<f64> = indices.iter().map(|&idx| life_exp[idx]).collect();
60+
let continent_pop: Vec<f64> = indices.iter().map(|&idx| pop[idx]).collect();
61+
let continent_countries: Vec<String> = indices.iter().map(|&idx| countries[idx].clone()).collect();
62+
let trace = Scatter::new(continent_gdp, continent_life)
63+
.mode(Mode::Markers)
64+
.name(continent.to_string())
65+
.text_array(continent_countries)
66+
.marker(
67+
Marker::new()
68+
.size_array(
69+
continent_pop
70+
.iter()
71+
.map(|&p| ((p / 1_000_000.0).min(60.0)) as usize)
72+
.collect(),
73+
)
74+
.color(*color)
75+
.opacity(0.6)
76+
.line(plotly::common::Line::new().width(1.0).color("white")),
77+
);
78+
plot.add_trace(trace);
79+
}
80+
81+
let theme_template = theme.build();
82+
plot.set_layout(
83+
Layout::new()
84+
.template(theme_template)
85+
.title(Title::from(format!(
86+
"Gapminder 2007: '{}' theme",
87+
theme_name(theme)
88+
)))
89+
.x_axis(
90+
plotly::layout::Axis::new()
91+
.title("GDP per capita (log scale)")
92+
.type_(plotly::layout::AxisType::Log),
93+
)
94+
.y_axis(plotly::layout::Axis::new().title("Life Expectancy"))
95+
.width(800)
96+
.height(600)
97+
.show_legend(true),
98+
);
99+
100+
let path = write_example_to_html(
101+
&plot,
102+
&format!("gapminder_{}", theme_name(theme).to_lowercase()),
103+
);
104+
if show {
105+
plot.show_html(path);
106+
}
107+
}
108+
109+
fn theme_name(theme: BuiltinTheme) -> &'static str {
110+
match theme {
111+
BuiltinTheme::Default => "plotly",
112+
BuiltinTheme::PlotlyWhite => "plotly_white",
113+
BuiltinTheme::PlotlyDark => "plotly_dark",
114+
BuiltinTheme::Seaborn => "seaborn",
115+
BuiltinTheme::SeabornWhitegrid => "seaborn_whitegrid",
116+
BuiltinTheme::SeabornDark => "seaborn_dark",
117+
BuiltinTheme::Matplotlib => "matplotlib",
118+
BuiltinTheme::Plotnine => "plotnine",
119+
}
120+
}
121+
122+
fn write_example_to_html(plot: &Plot, name: &str) -> String {
123+
std::fs::create_dir_all("./output").unwrap();
124+
// Write inline HTML
125+
let html = plot.to_inline_html(Some(name));
126+
let path = format!("./output/inline_{}.html", name);
127+
std::fs::write(path, html).unwrap();
128+
// Write standalone HTML
129+
let path = format!("./output/{}.html", name);
130+
plot.write_html(&path);
131+
path
132+
}
133+
134+
fn main() {
135+
// Create Gapminder-style plots with different themes, matching the Plotly documentation
136+
// Based on: https://plotly.com/python/templates/
137+
138+
// Create plots for each theme
139+
let themes = vec![
140+
BuiltinTheme::Default,
141+
BuiltinTheme::PlotlyWhite,
142+
BuiltinTheme::PlotlyDark,
143+
BuiltinTheme::Seaborn,
144+
BuiltinTheme::Matplotlib,
145+
BuiltinTheme::Plotnine,
146+
];
147+
148+
for theme in themes {
149+
create_gapminder_scatter_plot(theme, false);
150+
}
151+
152+
// Show one example (the default theme)
153+
create_gapminder_scatter_plot(BuiltinTheme::Default, true);
154+
}

0 commit comments

Comments
 (0)