a button that creates a rectangle and updates it into a pyplot chart
To upload files, please first save the app
import streamlit as st
import matplotlib.pyplot as plt
import numpy as np
# Initialize rectangle coordinates in session state if not present
if 'rectangles' not in st.session_state:
st.session_state.rectangles = []
# Create a figure and axis
fig, ax = plt.subplots()
# Button to add rectangle
if st.button('Add Rectangle'):
# Generate random position and size for rectangle
x = np.random.uniform(0, 8)
y = np.random.uniform(0, 8)
width = np.random.uniform(0.5, 2)
height = np.random.uniform(0.5, 2)
# Store rectangle parameters
st.session_state.rectangles.append((x, y, width, height))
# Plot all rectangles
for x, y, width, height in st.session_state.rectangles:
rect = plt.Rectangle((x, y), width, height, facecolor='blue', alpha=0.5)
ax.add_patch(rect)
# Set axis limits
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
# Display the plot
st.pyplot(fig)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?