Skip to content

Commit c6446ff

Browse files
authored
Merge pull request #545 from streamlit/streamlit-theme-charts
Add theming docs for Plotly, Altair, and Vega-Lite
2 parents 8dc357f + 1eac3d6 commit c6446ff

File tree

5 files changed

+233
-0
lines changed

5 files changed

+233
-0
lines changed

content/library/api/charts/altair_chart.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,119 @@ 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 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, theme="streamlit", use_container_width=True)
35+
with tab2:
36+
# Use the native Altair theme.
37+
st.altair_chart(chart, theme=None, use_container_width=True)
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! You can still make changes to your chart configurations. In other words, although we now enable the Streamlit theme by default, you can overwrite it with custom colors or fonts. For example, if you want a chart line to be green instead of the default red, you can do it!
45+
46+
Here's an example of an Altair chart where manual color passing is done and reflected:
47+
48+
<Collapse title="See the code">
49+
50+
```python
51+
import altair as alt
52+
import streamlit as st
53+
from vega_datasets import data
54+
55+
source = data.seattle_weather()
56+
57+
scale = alt.Scale(
58+
domain=["sun", "fog", "drizzle", "rain", "snow"],
59+
range=["#e7ba52", "#a7a7a7", "#aec7e8", "#1f77b4", "#9467bd"],
60+
)
61+
color = alt.Color("weather:N", scale=scale)
62+
63+
# We create two selections:
64+
# - a brush that is active on the top panel
65+
# - a multi-click that is active on the bottom panel
66+
brush = alt.selection_interval(encodings=["x"])
67+
click = alt.selection_multi(encodings=["color"])
68+
69+
# Top panel is scatter plot of temperature vs time
70+
points = (
71+
alt.Chart()
72+
.mark_point()
73+
.encode(
74+
alt.X("monthdate(date):T", title="Date"),
75+
alt.Y(
76+
"temp_max:Q",
77+
title="Maximum Daily Temperature (C)",
78+
scale=alt.Scale(domain=[-5, 40]),
79+
),
80+
color=alt.condition(brush, color, alt.value("lightgray")),
81+
size=alt.Size("precipitation:Q", scale=alt.Scale(range=[5, 200])),
82+
)
83+
.properties(width=550, height=300)
84+
.add_selection(brush)
85+
.transform_filter(click)
86+
)
87+
88+
# Bottom panel is a bar chart of weather type
89+
bars = (
90+
alt.Chart()
91+
.mark_bar()
92+
.encode(
93+
x="count()",
94+
y="weather:N",
95+
color=alt.condition(click, color, alt.value("lightgray")),
96+
)
97+
.transform_filter(brush)
98+
.properties(
99+
width=550,
100+
)
101+
.add_selection(click)
102+
)
103+
104+
chart = alt.vconcat(points, bars, data=source, title="Seattle Weather: 2012-2015")
105+
106+
tab1, tab2 = st.tabs(["Streamlit theme (default)", "Altair native theme"])
107+
108+
with tab1:
109+
st.altair_chart(chart, theme="streamlit", use_container_width=True)
110+
with tab2:
111+
st.altair_chart(chart, theme=None, use_container_width=True)
112+
```
113+
114+
</Collapse>
115+
116+
Notice how the custom colors are still reflected in the chart, even when the Streamlit theme is enabled 👇
117+
118+
<Cloud src="https://doc-altair-custom-colors.streamlit.app/?embed=true" height="675" />
119+
120+
For many more examples of Altair charts with and without the Streamlit theme, check out the [altair.streamlit.app](https://altair.streamlit.app).
121+
9122
### Annotating charts
10123

11124
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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,73 @@ 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 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, theme="streamlit", use_container_width=True)
39+
with tab2:
40+
# Use the native Plotly theme.
41+
st.plotly_chart(fig, theme=None, use_container_width=True)
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! You can still make changes to your chart configurations. In other words, although we now enable the Streamlit theme by default, you can overwrite it with custom colors or fonts. For example, if you want a chart line to be green instead of the default red, you can do it!
49+
50+
Here's an example of an Plotly chart where a custom color scale is defined and reflected:
51+
52+
```python
53+
import plotly.express as px
54+
import streamlit as st
55+
56+
st.subheader("Define a custom colorscale")
57+
df = px.data.iris()
58+
fig = px.scatter(
59+
df,
60+
x="sepal_width",
61+
y="sepal_length",
62+
color="sepal_length",
63+
color_continuous_scale="reds",
64+
)
65+
66+
tab1, tab2 = st.tabs(["Streamlit theme (default)", "Plotly native theme"])
67+
with tab1:
68+
st.plotly_chart(fig, theme="streamlit", use_conatiner_width=True)
69+
with tab2:
70+
st.plotly_chart(fig, theme=None, use_conatiner_width=True)
71+
```
72+
73+
Notice how the custom color scale is still reflected in the chart, even when the Streamlit theme is enabled 👇
74+
75+
<Cloud src="https://doc-plotly-custom-colors.streamlit.app/?embed=true" height="650" />
76+
77+
For many more examples of Plotly charts with and without the Streamlit theme, check out the [plotly.streamlit.app](https://plotly.streamlit.app).

content/library/api/charts/vega_lite_chart.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,53 @@ description: st.vega_lite_chart displays a chart using the Vega-Lite library.
55
---
66

77
<Autofunction function="streamlit.vega_lite_chart" />
8+
9+
### Theming
10+
11+
Vega-Lite 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 Streamlit theme is available from Streamlit 1.16.0 through the `theme="streamlit"` keyword argument. To disable it, and use Vega-Lite's native theme, use `theme=None` instead.
14+
15+
Let's look at an example of charts with the Streamlit theme and the native Vega-Lite theme:
16+
17+
```python
18+
import streamlit as st
19+
from vega_datasets import data
20+
21+
source = data.cars()
22+
23+
chart = {
24+
"mark": "point",
25+
"encoding": {
26+
"x": {
27+
"field": "Horsepower",
28+
"type": "quantitative",
29+
},
30+
"y": {
31+
"field": "Miles_per_Gallon",
32+
"type": "quantitative",
33+
},
34+
"color": {"field": "Origin", "type": "nominal"},
35+
"shape": {"field": "Origin", "type": "nominal"},
36+
},
37+
}
38+
39+
tab1, tab2 = st.tabs(["Streamlit theme (default)", "Vega-Lite native theme"])
40+
41+
with tab1:
42+
# Use the Streamlit theme.
43+
# This is the default. So you can also omit the theme argument.
44+
st.vega_lite_chart(
45+
source, chart, theme="streamlit", use_container_width=True
46+
)
47+
with tab2:
48+
st.vega_lite_chart(
49+
source, chart, theme=None, use_container_width=True
50+
)
51+
```
52+
53+
Click the tabs in the interactive app below to see the charts with the Streamlit theme enabled and disabled.
54+
55+
<Cloud src="https://doc-vega-lite-theme.streamlit.app/?embed=true" height="500" />
56+
57+
If you're wondering if your own customizations will still be taken into account, don't worry! You can still make changes to your chart configurations. In other words, although we now enable the Streamlit theme by default, you can overwrite it with custom colors or fonts. For example, if you want a chart line to be green instead of the default red, you can do it!

public/images/api/plotly_chart.jpg

-46.2 KB
Loading
124 KB
Loading

0 commit comments

Comments
 (0)