Create an app that generate and renders plotly charts
Drop files here
or click to upload
import streamlit as st
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
import pandas as pd
st.title("Plotly Charts Demo")
# Create sample data
np.random.seed(42)
dates = pd.date_range(start='2023-01-01', end='2023-12-31', freq='D')
values = np.random.normal(loc=100, scale=15, size=len(dates))
df = pd.DataFrame({
'date': dates,
'value': values
})
# Add some categorical data
categories = ['A', 'B', 'C', 'D']
df['category'] = np.random.choice(categories, size=len(df))
# Sidebar for chart selection
chart_type = st.sidebar.selectbox(
"Select Chart Type",
["Line Plot", "Scatter Plot", "Bar Chart", "Box Plot", "Histogram"]
)
# Display different charts based on selection
if chart_type == "Line Plot":
st.subheader("Line Plot")
fig = px.line(df, x='date', y='value',
title='Time Series Data')
st.plotly_chart(fig, use_container_width=True)
elif chart_type == "Scatter Plot":
st.subheader("Scatter Plot")
fig = px.scatter(df, x='date', y='value', color='category',
title='Scatter Plot with Categories')
st.plotly_chart(fig, use_container_width=True)
elif chart_type == "Bar Chart":
st.subheader("Bar Chart")
monthly_avg = df.groupby([df['date'].dt.month, 'category'])['value'].mean().reset_index()
monthly_avg['month'] = monthly_avg['date'].apply(lambda x: pd.datetime(2023, x, 1).strftime('%B'))
fig = px.bar(monthly_avg, x='month', y='value', color='category',
title='Monthly Average by Category',
barmode='group')
st.plotly_chart(fig, use_container_width=True)
elif chart_type == "Box Plot":
st.subheader("Box Plot")
fig = px.box(df, x='category', y='value',
title='Value Distribution by Category')
st.plotly_chart(fig, use_container_width=True)
else: # Histogram
st.subheader("Histogram")
fig = px.histogram(df, x='value', color='category',
title='Value Distribution',
marginal='box')
st.plotly_chart(fig, use_container_width=True)
# Add interactive features
st.sidebar.markdown("---")
st.sidebar.subheader("Chart Customization")
if st.sidebar.checkbox("Show Raw Data"):
st.subheader("Raw Data")
st.dataframe(df)
# Add 3D Scatter plot as a bonus
if st.sidebar.checkbox("Show 3D Scatter Plot"):
st.subheader("3D Scatter Plot")
df['extra_dim'] = np.random.normal(loc=50, scale=10, size=len(df))
fig = px.scatter_3d(df, x='date', y='value', z='extra_dim',
color='category',
title='3D Scatter Plot')
st.plotly_chart(fig, use_container_width=True)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?