Initial commit - Interactive Function Plotter
Browse files- .gradio/flagged/dataset1.csv +2 -0
- .gradio/flagged/output/7d18569bfba259d26ebd/plot.png +0 -0
- app.py +60 -0
- high_res_plot.png +0 -0
- plot.png +0 -0
.gradio/flagged/dataset1.csv
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Function (e.g., x**2, np.sin(x), np.exp(-x))",X Min,X Max,Resolution,output,timestamp
|
| 2 |
+
x**2,'-10,10,100,.gradio\flagged\output\7d18569bfba259d26ebd\plot.png,2025-03-12 13:02:38.051016
|
.gradio/flagged/output/7d18569bfba259d26ebd/plot.png
ADDED
|
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
|
| 6 |
+
try:
|
| 7 |
+
x_values = np.linspace(x_min, x_max, resolution)
|
| 8 |
+
functions = func_str.split(",")
|
| 9 |
+
|
| 10 |
+
plt.figure(figsize=(6, 4), dpi=300) # High-resolution image
|
| 11 |
+
|
| 12 |
+
for func_text in functions:
|
| 13 |
+
func_text = func_text.strip()
|
| 14 |
+
func = lambda x: eval(func_text, {"x": x, "np": np})
|
| 15 |
+
y_values = func(x_values)
|
| 16 |
+
plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=color, linestyle=linestyle)
|
| 17 |
+
|
| 18 |
+
plt.xlabel("x")
|
| 19 |
+
plt.ylabel("f(x)")
|
| 20 |
+
plt.title("Function Plot")
|
| 21 |
+
plt.legend()
|
| 22 |
+
|
| 23 |
+
if grid:
|
| 24 |
+
plt.grid()
|
| 25 |
+
|
| 26 |
+
plt.savefig("high_res_plot.png", dpi=300) # Save high-res plot
|
| 27 |
+
plt.close()
|
| 28 |
+
return "high_res_plot.png", "high_res_plot.png"
|
| 29 |
+
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Error: {e}", None
|
| 32 |
+
|
| 33 |
+
# Using gr.Blocks() for better UI layout
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("# Interactive Function Plotter 📈")
|
| 36 |
+
|
| 37 |
+
with gr.Row():
|
| 38 |
+
with gr.Column():
|
| 39 |
+
func_str = gr.Textbox(label="Function (e.g., x**2, np.sin(x), np.exp(-x))")
|
| 40 |
+
x_min = gr.Number(label="X Min", value=-10)
|
| 41 |
+
x_max = gr.Number(label="X Max", value=10)
|
| 42 |
+
resolution = gr.Slider(10, 1000, step=10, label="Resolution", value=100)
|
| 43 |
+
color = gr.ColorPicker(label="Line Color")
|
| 44 |
+
linestyle = gr.Dropdown(["solid", "dashed", "dotted", "dashdot"], label="Line Style")
|
| 45 |
+
grid = gr.Checkbox(label="Show Grid", value=True)
|
| 46 |
+
submit_button = gr.Button("Plot Function")
|
| 47 |
+
|
| 48 |
+
with gr.Column():
|
| 49 |
+
output_image = gr.Image(label="Function Plot")
|
| 50 |
+
download_button = gr.File(label="Download High-Res Plot")
|
| 51 |
+
|
| 52 |
+
# Ensure this line is correctly aligned within gr.Blocks()
|
| 53 |
+
submit_button.click(
|
| 54 |
+
plot_function,
|
| 55 |
+
inputs=[func_str, x_min, x_max, resolution, color, linestyle, grid],
|
| 56 |
+
outputs=[output_image, download_button]
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Ensure this is correctly aligned with gr.Blocks()
|
| 60 |
+
demo.launch()
|
high_res_plot.png
ADDED
|
plot.png
ADDED
|