Skip to content

Commit e1a0f46

Browse files
committed
Add theming docs for Plotly and Altair
1 parent 36dfe13 commit e1a0f46

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

content/library/api/charts/altair_chart.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,117 @@ description: st.altair_chart displays a chart using the Altair library.
66

77
<Autofunction function="streamlit.altair_chart" />
88

9+
### Theming
10+
11+
Altair charts are displayed using the Streamlit theme by default. This theme is sleek, user-friendly, and incorporates Streamlit's color palette. The added benefit is that your charts better integrate with the rest of your app's design.
12+
13+
The new Streamlit theme is available from Streamlit 1.16.0 through the `theme="streamlit"` keyword argument. To disable it, and use Altair's native theme, use `theme=None` instead.
14+
15+
Let's look at an example of charts with the Streamlit theme and the native Altair theme:
16+
17+
```python
18+
import altair as alt
19+
from vega_datasets import data
20+
21+
source = data.cars()
22+
23+
chart = alt.Chart(source).mark_circle().encode(
24+
x='Horsepower',
25+
y='Miles_per_Gallon',
26+
color='Origin',
27+
).interactive()
28+
29+
tab1, tab2 = st.tabs(["Streamlit theme (default)", "Altair native theme"])
30+
31+
with tab1:
32+
# Use the Streamlit theme.
33+
# This is the default. So you can also omit the theme argument.
34+
st.altair_chart(chart, use_container_width=use_container_width, theme="streamlit")
35+
with tab2:
36+
# Use the native Altair theme.
37+
st.altair_chart(chart, use_container_width=use_container_width, theme=None)
38+
```
39+
40+
Click the tabs in the interactive app below to see the charts with the Streamlit theme enabled and disabled.
41+
42+
<Cloud src="https://doc-altair-chart.streamlit.app/?embed=true" height="500" />
43+
44+
If you're wondering if your own customizations will still be taken into account, don't worry! If you're an experienced Altair user and like to have your own special customizations, your changes in the chart configurations will still be taken into account. Here's an example of an Altair chart where manual color passing is done and reflected:
45+
46+
<Collapse title="See the code">
47+
48+
```python
49+
import altair as alt
50+
import streamlit as st
51+
from vega_datasets import data
52+
53+
source = data.seattle_weather()
54+
55+
scale = alt.Scale(
56+
domain=["sun", "fog", "drizzle", "rain", "snow"],
57+
range=["#e7ba52", "#a7a7a7", "#aec7e8", "#1f77b4", "#9467bd"],
58+
)
59+
color = alt.Color("weather:N", scale=scale)
60+
61+
# We create two selections:
62+
# - a brush that is active on the top panel
63+
# - a multi-click that is active on the bottom panel
64+
brush = alt.selection_interval(encodings=["x"])
65+
click = alt.selection_multi(encodings=["color"])
66+
67+
# Top panel is scatter plot of temperature vs time
68+
points = (
69+
alt.Chart()
70+
.mark_point()
71+
.encode(
72+
alt.X("monthdate(date):T", title="Date"),
73+
alt.Y(
74+
"temp_max:Q",
75+
title="Maximum Daily Temperature (C)",
76+
scale=alt.Scale(domain=[-5, 40]),
77+
),
78+
color=alt.condition(brush, color, alt.value("lightgray")),
79+
size=alt.Size("precipitation:Q", scale=alt.Scale(range=[5, 200])),
80+
)
81+
.properties(width=550, height=300)
82+
.add_selection(brush)
83+
.transform_filter(click)
84+
)
85+
86+
# Bottom panel is a bar chart of weather type
87+
bars = (
88+
alt.Chart()
89+
.mark_bar()
90+
.encode(
91+
x="count()",
92+
y="weather:N",
93+
color=alt.condition(click, color, alt.value("lightgray")),
94+
)
95+
.transform_filter(brush)
96+
.properties(
97+
width=550,
98+
)
99+
.add_selection(click)
100+
)
101+
102+
chart = alt.vconcat(points, bars, data=source, title="Seattle Weather: 2012-2015")
103+
104+
tab1, tab2 = st.tabs(["Streamlit theme (default)", "Altair native theme"])
105+
106+
with tab1:
107+
st.altair_chart(chart, use_container_width=True theme="streamlit")
108+
with tab2:
109+
st.altair_chart(chart, use_container_width=True, theme=None)
110+
```
111+
112+
</Collapse>
113+
114+
Notice how the custom colors are still reflected in the chart, even when the Streamlit theme is enabled 👇
115+
116+
<Cloud src="https://doc-altair-custom-colors.streamlit.app/?embed=true" height="675" />
117+
118+
For many more examples of Altair charts with and without the Streamlit theme, check out the [altair.streamlit.app](https://altair.streamlit.app).
119+
9120
### Annotating charts
10121

11122
Altair also allows you to annotate your charts with text, images, and emojis. You can do this by creating [layered charts](https://altair-viz.github.io/user_guide/compound_charts.html#layered-charts), which let you overlay two different charts on top of each other. The idea is to use the first chart to show the data, and the second chart to show the annotations. The second chart can then be overlaid on top of the first chart using the `+` operator to create a layered chart.

content/library/api/charts/plotly_chart.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,71 @@ description: st.plotly_chart displays an interactive Plotly chart.
55
---
66

77
<Autofunction function="streamlit.plotly_chart" />
8+
9+
### Theming
10+
11+
Plotly charts are displayed using the Streamlit theme by default. This theme is sleek, user-friendly, and incorporates Streamlit's color palette. The added benefit is that your charts better integrate with the rest of your app's design.
12+
13+
The new Streamlit theme is available from Streamlit 1.16.0 through the `theme="streamlit"` keyword argument. To disable it, and use Plotly's native theme, use `theme=None` instead.
14+
15+
Let's look at an example of charts with the Streamlit theme and the native Plotly theme:
16+
17+
```python
18+
import plotly.express as px
19+
import streamlit as st
20+
21+
df = px.data.gapminder()
22+
23+
fig = px.scatter(
24+
df.query("year==2007"),
25+
x="gdpPercap",
26+
y="lifeExp",
27+
size="pop",
28+
color="continent",
29+
hover_name="country",
30+
log_x=True,
31+
size_max=60,
32+
)
33+
34+
tab1, tab2 = st.tabs(["Streamlit theme (default)", "Plotly native theme"])
35+
with tab1:
36+
# Use the Streamlit theme.
37+
# This is the default. So you can also omit the theme argument.
38+
st.plotly_chart(fig, use_container_width=True, theme="streamlit")
39+
with tab2:
40+
# Use the native Plotly theme.
41+
st.plotly_chart(fig, use_container_width=True, theme=None)
42+
```
43+
44+
Click the tabs in the interactive app below to see the charts with the Streamlit theme enabled and disabled.
45+
46+
<Cloud src="https://doc-plotly-chart-theme.streamlit.app/?embed=true" height="525" />
47+
48+
If you're wondering if your own customizations will still be taken into account, don't worry! If you're an experienced Plotly user and like to have your own special customizations, your changes in the chart configurations will still be taken into account. Here's an example of an Plotly chart where a custom color scale is defined and reflected:
49+
50+
```python
51+
import plotly.express as px
52+
import streamlit as st
53+
54+
st.subheader("Define a custom colorscale")
55+
df = px.data.iris()
56+
fig = px.scatter(
57+
df,
58+
x="sepal_width",
59+
y="sepal_length",
60+
color="sepal_length",
61+
color_continuous_scale="reds",
62+
)
63+
64+
tab1, tab2 = st.tabs(["Streamlit theme (default)", "Plotly native theme"])
65+
with tab1:
66+
st.plotly_chart(fig, use_conatiner_width=use_conatiner_width, theme="streamlit")
67+
with tab2:
68+
st.plotly_chart(fig, use_conatiner_width=use_conatiner_width, theme=None)
69+
```
70+
71+
Notice how the custom color scale is still reflected in the chart, even when the Streamlit theme is enabled 👇
72+
73+
<Cloud src="https://doc-plotly-custom-colors.streamlit.app/?embed=true" height="650" />
74+
75+
For many more examples of Plotly charts with and without the Streamlit theme, check out the [plotly.streamlit.app](https://plotly.streamlit.app).

0 commit comments

Comments
 (0)