eaglelandsonce commited on
Commit
01e1a9d
1 Parent(s): e6adb31

Update pages/40_simpleline.py

Browse files
Files changed (1) hide show
  1. pages/40_simpleline.py +27 -17
pages/40_simpleline.py CHANGED
@@ -1,37 +1,47 @@
1
  import streamlit as st
2
- import matplotlib.pyplot as plt
3
 
4
  # Title
5
  st.title("Interactive Equation Plotter")
6
 
7
  # Sidebar sliders
8
  st.sidebar.header("Controls")
9
- x = st.sidebar.slider('X position', min_value=-100.0, max_value=100.0, value=0.0, step=1.0)
10
  w = st.sidebar.slider('W (slope)', min_value=-10.0, max_value=10.0, value=1.0, step=0.1)
11
  b = st.sidebar.slider('B (intercept)', min_value=-100.0, max_value=100.0, value=0.0, step=1.0)
12
 
 
 
 
13
  # Calculate y based on the equation y = w * x + b
14
  y = w * x + b
15
 
16
  # Create the plot
17
- fig, ax = plt.subplots(figsize=(6, 4))
18
- fig.subplots_adjust(top=0.8, bottom=0.1)
19
 
20
  # Plot for the y-axis (top line)
21
- ax.hlines(1, -100, 100, color='blue', linestyle='--') # Y-axis
22
- ax.plot(y, 1, 'ro') # Plot the y point
23
 
24
  # Plot for the x-axis (bottom line)
25
- ax.hlines(-1, -100, 100, color='blue', linestyle='--') # X-axis
26
- ax.plot(x, -1, 'ro') # Plot the x point
27
-
28
- # Set plot limits and labels
29
- ax.set_xlim(-100, 100)
30
- ax.set_ylim(-2, 2)
31
- ax.set_xlabel('X-axis and Y-axis')
32
- ax.set_yticks([]) # Hide y-axis ticks
33
- ax.set_title(f'y = {w} * x + {b}')
34
- ax.grid(True)
 
 
 
 
 
 
35
 
36
  # Display the plot in Streamlit
37
- st.pyplot(fig)
 
 
 
 
1
  import streamlit as st
2
+ import plotly.graph_objects as go
3
 
4
  # Title
5
  st.title("Interactive Equation Plotter")
6
 
7
  # Sidebar sliders
8
  st.sidebar.header("Controls")
 
9
  w = st.sidebar.slider('W (slope)', min_value=-10.0, max_value=10.0, value=1.0, step=0.1)
10
  b = st.sidebar.slider('B (intercept)', min_value=-100.0, max_value=100.0, value=0.0, step=1.0)
11
 
12
+ # Initial x position
13
+ x = st.slider('X position', min_value=-100.0, max_value=100.0, value=0.0, step=1.0)
14
+
15
  # Calculate y based on the equation y = w * x + b
16
  y = w * x + b
17
 
18
  # Create the plot
19
+ fig = go.Figure()
 
20
 
21
  # Plot for the y-axis (top line)
22
+ fig.add_shape(type="line", x0=-100, x1=100, y0=1, y1=1,
23
+ line=dict(color="blue", width=2, dash="dash"))
24
 
25
  # Plot for the x-axis (bottom line)
26
+ fig.add_shape(type="line", x0=-100, x1=100, y0=-1, y1=-1,
27
+ line=dict(color="blue", width=2, dash="dash"))
28
+
29
+ # Plot the x and y points
30
+ fig.add_trace(go.Scatter(x=[x], y=[-1], mode='markers', marker=dict(color='red', size=10), name="X point"))
31
+ fig.add_trace(go.Scatter(x=[y], y=[1], mode='markers', marker=dict(color='red', size=10), name="Y point"))
32
+
33
+ # Update the layout
34
+ fig.update_layout(
35
+ title=f'y = {w} * x + {b}',
36
+ xaxis=dict(range=[-100, 100]),
37
+ yaxis=dict(range=[-2, 2]),
38
+ showlegend=False,
39
+ height=400,
40
+ margin=dict(t=50, b=10)
41
+ )
42
 
43
  # Display the plot in Streamlit
44
+ st.plotly_chart(fig)
45
+
46
+ # Add instruction to click and drag the point on x-axis
47
+ st.write("Drag the red dot on the x-axis to change the x position.")