Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions calc-backend/graph-gen/graph-deriv.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,26 @@ def derivative(func, x, h=1e-5):
# Initialize the Plotly figure
fig = go.Figure()

# Adjust padding for final layout
pad = min(np.abs(np.min(f_vals)), np.abs(np.max(f_vals))) + (max(np.abs(np.min(f_vals)), np.abs(np.max(f_vals))) // 20)
# Calculate y-axis padding
f_range = np.max(f_vals) - np.min(f_vals)
# 5% of range or at least 0.1
pad = max(f_range * 0.05, 0.1)

# Set the final y-axis range with padding
y_min = np.min(f_vals) - pad
y_max = np.max(f_vals) + pad
# No padding on top to avoid cutting off curve
y_max = np.max(f_vals)

# Calculate x-axis padding
x_range_val = x_range[1] - x_range[0]
x_pad = max(x_range_val * 0.05, 0.1)

# Set the final x-axis range with padding
x_min = x_range[0] - x_pad
x_max = x_range[1] + x_pad

# Vertical line at x=0 (y-axis)
if x_range[0] < 0 < x_range[1]:
if x_min < 0 < x_max:
fig.add_trace(go.Scatter(
x=[0, 0],
y=[y_min, y_max],
Expand All @@ -64,7 +75,7 @@ def derivative(func, x, h=1e-5):
# Horizontal line at y=0 (x-axis)
if y_min < 0 < y_max:
fig.add_trace(go.Scatter(
x=[x_range[0], x_range[1]],
x=[x_min, x_max],
y=[0, 0],
mode='lines',
line=dict(color='silver', width=2),
Expand Down Expand Up @@ -101,12 +112,12 @@ def derivative(func, x, h=1e-5):
frame_data=[]

# Conditionally add vertical line at x=0
if x_range[0] < 0 < x_range[1]:
if x_min < 0 < x_max:
frame_data.append(go.Scatter(x=[0, 0], y=[y_min, y_max]))

# Conditionally add horizontal line at y=0
if y_min < 0 < y_max:
frame_data.append(go.Scatter(x=[x_range[0], x_range[1]]))
frame_data.append(go.Scatter(x=[x_min, x_max], y=[0, 0]))

# Placeholder for the function trace (remains unchanged)
frame_data.append(go.Scatter())
Expand Down Expand Up @@ -149,7 +160,7 @@ def derivative(func, x, h=1e-5):
fig.update_layout(
xaxis_title='x-axis',
yaxis_title='y-axis',
xaxis=dict(range=[x_range[0], x_range[1]], fixedrange=True),
xaxis=dict(range=[x_min, x_max], fixedrange=True),
yaxis=dict(range=[y_min, y_max], fixedrange=True),
sliders=sliders,
uirevision='static',
Expand Down
6 changes: 4 additions & 2 deletions calc-backend/graph-gen/graph-integral.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ def generate_bars(n_bars):

fig.update_traces(hovertemplate="(%{x:.2f}, %{y:.2f})", name="Function", showlegend=False)

# Adjust padding for final layout
pad = min(np.abs(np.min(y_line)), np.abs(np.max(y_line))) + (max(np.abs(np.min(y_line)), np.abs(np.max(y_line))) // 20)
# Calculate y-axis padding
f_range = np.max(y_line) - np.min(y_line)
# 5% of range or at least 0.1
pad = max(f_range * 0.05, 0.1)

# Set the final y-axis range with padding
y_min = np.min(y_line) - pad
Expand Down