Create a Streamlit application with 2 diagrams and 3 widgets that operate on both diagrams. Two widgets consist of sets of (multi) select boxes, and the third is a number slider. The widgets should be on the left-hand side of the page, and the diagrams on the right-hand side stacked on top of each other.
To upload files, please first save the app
import streamlit as st
import plotly.express as px
import pandas as pd
import numpy as np
# Set page configuration to wide mode
st.set_page_config(layout="wide")
# Create sample data
np.random.seed(42)
n_points = 1000
data = pd.DataFrame({
'x': np.random.normal(0, 1, n_points),
'y': np.random.normal(0, 1, n_points),
'category': np.random.choice(['A', 'B', 'C', 'D'], n_points),
'subcategory': np.random.choice(['X', 'Y', 'Z'], n_points),
'value': np.random.uniform(0, 100, n_points)
})
# Create layout with sidebar for widgets
st.title("Interactive Dual Diagram Dashboard")
# Sidebar widgets
with st.sidebar:
st.header("Controls")
# First multi-select widget for categories
selected_categories = st.multiselect(
"Select Categories",
options=sorted(data['category'].unique()),
default=sorted(data['category'].unique())
)
# Second multi-select widget for subcategories
selected_subcategories = st.multiselect(
"Select Subcategories",
options=sorted(data['subcategory'].unique()),
default=sorted(data['subcategory'].unique())
)
# Number slider
point_size = st.slider(
"Point Size",
min_value=1,
max_value=20,
value=8,
help="Adjust the size of points in both plots"
)
# Filter data based on selections
filtered_data = data[
(data['category'].isin(selected_categories)) &
(data['subcategory'].isin(selected_subcategories))
]
# Create two columns for the plots
col1, col2 = st.columns(2)
# First plot: Scatter plot
scatter_fig = px.scatter(
filtered_data,
x='x',
y='y',
color='category',
symbol='subcategory',
size=[point_size] * len(filtered_data),
title='Interactive Scatter Plot'
)
scatter_fig.update_layout(height=400)
# Second plot: Box plot
box_fig = px.box(
filtered_data,
x='category',
y='value',
color='subcategory',
title='Distribution by Category and Subcategory'
)
box_fig.update_layout(height=400)
# Display plots
st.plotly_chart(scatter_fig, use_container_width=True)
st.plotly_chart(box_fig, use_container_width=True)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?