Skip to content

Instantly share code, notes, and snippets.

@stanlee321
Forked from tvst/Example plots with Streamlit
Created November 15, 2022 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stanlee321/e162a308ebeec41ed95347442e62ed03 to your computer and use it in GitHub Desktop.
Save stanlee321/e162a308ebeec41ed95347442e62ed03 to your computer and use it in GitHub Desktop.
import streamlit as st
import pandas as pd
from vega_datasets import data
"""
# Using different charting libraries
"""
@st.cache
def get_data():
return pd.read_json(data.cars.url)
df = get_data()
"""
Streamlit supports different charting libraries, like:
* Matplotlib
* Seaborn
* Altair
* Plotly
* Bokeh
"""
"""
## Altair example
"""
with st.echo(code_location='below'):
import altair as alt
st.write(alt.Chart(df).mark_point().encode(
# The notation below is shorthand for:
# x = alt.X("Acceleration", type="quantitative", title="Acceleration"),
x="Acceleration:Q",
y=alt.Y("Miles_per_Gallon", type="quantitative", title="Miles per gallon"),
))
"""
## Plotly example
"""
with st.echo(code_location='below'):
import plotly.express as px
fig = px.scatter(
x=df["Acceleration"],
y=df["Miles_per_Gallon"],
)
fig.update_layout(
xaxis_title="Acceleration",
yaxis_title="Miles per gallon",
)
st.write(fig)
"""
## Matplotlib example
"""
with st.echo(code_location='below'):
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(
df["Acceleration"],
df["Miles_per_Gallon"],
)
ax.set_xlabel("Acceleration")
ax.set_ylabel("Miles per gallon")
st.write(fig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment