create an app that mixes columns and matplotlib, generate a function that generates random data and a random plot, then show those plots, it's important to make the function return a figure object so we can plot all of them at the same time
To upload files, please first save the app
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
# Function to generate random data and a plot
def generate_random_plot():
# Generate random data
x = np.linspace(0, 10, 100)
y = np.random.rand(100)
color = np.random.rand(3,) # Random color for the line
# Randomly choose between histogram and scatterplot
plot_type = np.random.choice(['scatter', 'histogram'])
if plot_type == 'scatter':
# Create a scatter plot
fig, ax = plt.subplots()
ax.scatter(x, y, color=color, label='Random Scatter Data')
ax.set_title('Random Scatter Plot')
else:
# Create a histogram
fig, ax = plt.subplots()
ax.hist(y, bins=10, color=color, label='Random Histogram Data', alpha=0.7)
ax.set_title('Random Histogram Plot')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.legend()
return fig
# Streamlit app
st.title('Random Data Plotter')
# Generate multiple random plots
num_plots = st.slider('Select number of plots to generate per column:', 1, 5)
col1, col2, col3 = st.columns(3)
for i in range(num_plots):
with col1:
fig = generate_random_plot()
st.pyplot(fig)
with col2:
fig = generate_random_plot()
st.pyplot(fig)
with col3:
fig = generate_random_plot()
st.pyplot(fig)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?