@@ -38,7 +38,7 @@ def derivative(func, x, h=1e-5):
3838 return (func (x + h ) - func (x - h )) / (2 * h )
3939
4040# Create an array of x values and evaluate f(x) for these values.
41- x_vals = np .arange (x_range [0 ], x_range [1 ] + 0.05 , 0.05 )
41+ x_vals = np .linspace (x_range [0 ], x_range [1 ], num = 51 )
4242f_vals = f (x_vals )
4343
4444# Initialize the Plotly figure and plot the original function.
@@ -57,28 +57,32 @@ def derivative(func, x, h=1e-5):
5757 name = 'Point' ))
5858
5959# Set the initial layout title with the slope at the starting point.
60- fig .update_layout (title = f"Slope Value = { derivative (f , x0 ):.2f} " )
60+ fig .update_layout (title = (f'Slope Value = { derivative (f , x0 ):.4f} ' ), title_font_size = 16 )
61+
62+ # Get the initial y-axis range
63+ initial_yaxis = fig .layout ['yaxis_range' ]
6164
6265# Create frames for the animation slider.
6366frames = []
6467for slider_val in x_vals :
6568 # Compute the tangent line at each slider value using our numerical derivative.
66- tan_y = derivative (f , slider_val ) * (x_vals - slider_val ) + f (slider_val )
69+ save_deriv = derivative (f , slider_val )
70+ tan_y = save_deriv * (x_vals - slider_val ) + f (slider_val )
6771 frames .append (go .Frame (
6872 data = [
6973 go .Scatter (), # Placeholder for the function trace (remains unchanged).
7074 go .Scatter (x = x_vals , y = tan_y ), # Tangent line.
7175 go .Scatter (x = [slider_val ], y = [f (slider_val )]) # Point of tangency.
7276 ],
73- layout = go .Layout (title = { 'text' : f" Slope Value = { derivative ( f , slider_val ):.2f } " }),
77+ layout = go .Layout (title = ( f' Slope Value = { save_deriv :.4f } ' ), yaxis_range = initial_yaxis , yaxis = { 'autorange' : False }),
7478 name = str (slider_val )
7579 ))
7680fig .frames = frames
7781
7882# Create a slider to control the animation.
7983sliders = [dict (
8084 active = 0 ,
81- currentvalue = { "visible" : False } ,
85+ currentvalue = dict ( prefix = "Slider" , font = { 'size' : 14 }) ,
8286 ticklen = 0 , # Hides the line ticks (not enough alone)
8387 len = 1.0 ,
8488 y = - 0.05 ,
@@ -88,14 +92,16 @@ def derivative(func, x, h=1e-5):
8892 args = [
8993 [frame .name ],
9094 dict (mode = 'immediate' ,
91- frame = dict (duration = 40 , redraw = True ),
92- transition = dict ( duration = 0 ) )
95+ frame = dict (duration = 0 , redraw = True ),
96+ )
9397 ],
9498 label = "" , # Hides numbers
9599 )
96100 for frame in frames
97101 ],
98102 tickcolor = 'rgba(0,0,0,0)' ,
103+ bordercolor = '#949fb3' ,
104+ pad = {"t" : 10 , "b" : 10 },
99105)]
100106
101107# Adjust padding and final layout settings.
@@ -106,7 +112,8 @@ def derivative(func, x, h=1e-5):
106112 xaxis = dict (range = [x_range [0 ], x_range [1 ]], fixedrange = True ),
107113 yaxis = dict (range = [np .min (f_vals ) - pad , np .max (f_vals ) + pad ], fixedrange = True ),
108114 sliders = sliders ,
109- uirevision = 'static'
115+ uirevision = 'static' ,
116+ margin = dict (t = 50 , r = 0 ,l = 60 ),
110117)
111118
112119fig_json = fig .to_json (pretty = True )
0 commit comments