diff --git a/content/library/api/charts/altair_chart.md b/content/library/api/charts/altair_chart.md index c6924e611..87f9fe9a5 100644 --- a/content/library/api/charts/altair_chart.md +++ b/content/library/api/charts/altair_chart.md @@ -6,6 +6,119 @@ description: st.altair_chart displays a chart using the Altair library. +### Theming + +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. + +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. + +Let's look at an example of charts with the Streamlit theme and the native Altair theme: + +```python +import altair as alt +from vega_datasets import data + +source = data.cars() + +chart = alt.Chart(source).mark_circle().encode( + x='Horsepower', + y='Miles_per_Gallon', + color='Origin', +).interactive() + +tab1, tab2 = st.tabs(["Streamlit theme (default)", "Altair native theme"]) + +with tab1: + # Use the Streamlit theme. + # This is the default. So you can also omit the theme argument. + st.altair_chart(chart, theme="streamlit", use_container_width=True) +with tab2: + # Use the native Altair theme. + st.altair_chart(chart, theme=None, use_container_width=True) +``` + +Click the tabs in the interactive app below to see the charts with the Streamlit theme enabled and disabled. + + + +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! + +Here's an example of an Altair chart where manual color passing is done and reflected: + + + +```python +import altair as alt +import streamlit as st +from vega_datasets import data + +source = data.seattle_weather() + +scale = alt.Scale( + domain=["sun", "fog", "drizzle", "rain", "snow"], + range=["#e7ba52", "#a7a7a7", "#aec7e8", "#1f77b4", "#9467bd"], +) +color = alt.Color("weather:N", scale=scale) + +# We create two selections: +# - a brush that is active on the top panel +# - a multi-click that is active on the bottom panel +brush = alt.selection_interval(encodings=["x"]) +click = alt.selection_multi(encodings=["color"]) + +# Top panel is scatter plot of temperature vs time +points = ( + alt.Chart() + .mark_point() + .encode( + alt.X("monthdate(date):T", title="Date"), + alt.Y( + "temp_max:Q", + title="Maximum Daily Temperature (C)", + scale=alt.Scale(domain=[-5, 40]), + ), + color=alt.condition(brush, color, alt.value("lightgray")), + size=alt.Size("precipitation:Q", scale=alt.Scale(range=[5, 200])), + ) + .properties(width=550, height=300) + .add_selection(brush) + .transform_filter(click) +) + +# Bottom panel is a bar chart of weather type +bars = ( + alt.Chart() + .mark_bar() + .encode( + x="count()", + y="weather:N", + color=alt.condition(click, color, alt.value("lightgray")), + ) + .transform_filter(brush) + .properties( + width=550, + ) + .add_selection(click) +) + +chart = alt.vconcat(points, bars, data=source, title="Seattle Weather: 2012-2015") + +tab1, tab2 = st.tabs(["Streamlit theme (default)", "Altair native theme"]) + +with tab1: + st.altair_chart(chart, theme="streamlit", use_container_width=True) +with tab2: + st.altair_chart(chart, theme=None, use_container_width=True) +``` + + + +Notice how the custom colors are still reflected in the chart, even when the Streamlit theme is enabled 👇 + + + +For many more examples of Altair charts with and without the Streamlit theme, check out the [altair.streamlit.app](https://altair.streamlit.app). + ### Annotating charts 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. diff --git a/content/library/api/charts/plotly_chart.md b/content/library/api/charts/plotly_chart.md index 1b38d4fce..953caac9f 100644 --- a/content/library/api/charts/plotly_chart.md +++ b/content/library/api/charts/plotly_chart.md @@ -5,3 +5,73 @@ description: st.plotly_chart displays an interactive Plotly chart. --- + +### Theming + +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. + +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. + +Let's look at an example of charts with the Streamlit theme and the native Plotly theme: + +```python +import plotly.express as px +import streamlit as st + +df = px.data.gapminder() + +fig = px.scatter( + df.query("year==2007"), + x="gdpPercap", + y="lifeExp", + size="pop", + color="continent", + hover_name="country", + log_x=True, + size_max=60, +) + +tab1, tab2 = st.tabs(["Streamlit theme (default)", "Plotly native theme"]) +with tab1: + # Use the Streamlit theme. + # This is the default. So you can also omit the theme argument. + st.plotly_chart(fig, theme="streamlit", use_container_width=True) +with tab2: + # Use the native Plotly theme. + st.plotly_chart(fig, theme=None, use_container_width=True) +``` + +Click the tabs in the interactive app below to see the charts with the Streamlit theme enabled and disabled. + + + +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! + +Here's an example of an Plotly chart where a custom color scale is defined and reflected: + +```python +import plotly.express as px +import streamlit as st + +st.subheader("Define a custom colorscale") +df = px.data.iris() +fig = px.scatter( + df, + x="sepal_width", + y="sepal_length", + color="sepal_length", + color_continuous_scale="reds", +) + +tab1, tab2 = st.tabs(["Streamlit theme (default)", "Plotly native theme"]) +with tab1: + st.plotly_chart(fig, theme="streamlit", use_conatiner_width=True) +with tab2: + st.plotly_chart(fig, theme=None, use_conatiner_width=True) +``` + +Notice how the custom color scale is still reflected in the chart, even when the Streamlit theme is enabled 👇 + + + +For many more examples of Plotly charts with and without the Streamlit theme, check out the [plotly.streamlit.app](https://plotly.streamlit.app). diff --git a/content/library/api/charts/vega_lite_chart.md b/content/library/api/charts/vega_lite_chart.md index 507dcdb94..aa008e341 100644 --- a/content/library/api/charts/vega_lite_chart.md +++ b/content/library/api/charts/vega_lite_chart.md @@ -5,3 +5,53 @@ description: st.vega_lite_chart displays a chart using the Vega-Lite library. --- + +### Theming + +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. + +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. + +Let's look at an example of charts with the Streamlit theme and the native Vega-Lite theme: + +```python +import streamlit as st +from vega_datasets import data + +source = data.cars() + +chart = { + "mark": "point", + "encoding": { + "x": { + "field": "Horsepower", + "type": "quantitative", + }, + "y": { + "field": "Miles_per_Gallon", + "type": "quantitative", + }, + "color": {"field": "Origin", "type": "nominal"}, + "shape": {"field": "Origin", "type": "nominal"}, + }, +} + +tab1, tab2 = st.tabs(["Streamlit theme (default)", "Vega-Lite native theme"]) + +with tab1: + # Use the Streamlit theme. + # This is the default. So you can also omit the theme argument. + st.vega_lite_chart( + source, chart, theme="streamlit", use_container_width=True + ) +with tab2: + st.vega_lite_chart( + source, chart, theme=None, use_container_width=True + ) +``` + +Click the tabs in the interactive app below to see the charts with the Streamlit theme enabled and disabled. + + + +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! diff --git a/public/images/api/plotly_chart.jpg b/public/images/api/plotly_chart.jpg index 84929b70c..1251357d1 100644 Binary files a/public/images/api/plotly_chart.jpg and b/public/images/api/plotly_chart.jpg differ diff --git a/public/images/api/vega_lite_chart.jpg b/public/images/api/vega_lite_chart.jpg index 5ac57fc7c..477ab434e 100644 Binary files a/public/images/api/vega_lite_chart.jpg and b/public/images/api/vega_lite_chart.jpg differ