Spaces:
Sleeping
Sleeping
import streamlit as st | |
import matplotlib.pyplot as plt | |
# Title | |
st.title("Interactive Equation Plotter") | |
# Sidebar sliders | |
st.sidebar.header("Controls") | |
x = st.sidebar.slider('X position', min_value=-100.0, max_value=100.0, value=0.0, step=1.0) | |
w = st.sidebar.slider('W (slope)', min_value=-10.0, max_value=10.0, value=1.0, step=0.1) | |
b = st.sidebar.slider('B (intercept)', min_value=-100.0, max_value=100.0, value=0.0, step=1.0) | |
# Calculate y based on the equation y = w * x + b | |
y = w * x + b | |
# Create the plot | |
fig, ax = plt.subplots(figsize=(6, 4)) | |
fig.subplots_adjust(top=0.8, bottom=0.1) | |
# Plot for the y-axis (top line) | |
ax.hlines(1, -100, 100, color='blue', linestyle='--') # Y-axis | |
ax.plot(y, 1, 'ro') # Plot the y point | |
# Plot for the x-axis (bottom line) | |
ax.hlines(-1, -100, 100, color='blue', linestyle='--') # X-axis | |
ax.plot(x, -1, 'ro') # Plot the x point | |
# Set plot limits and labels | |
ax.set_xlim(-100, 100) | |
ax.set_ylim(-2, 2) | |
ax.set_xlabel('X-axis and Y-axis') | |
ax.set_yticks([]) # Hide y-axis ticks | |
ax.set_title(f'y = {w} * x + {b}') | |
ax.grid(True) | |
# Display the plot in Streamlit | |
st.pyplot(fig) | |