File size: 2,969 Bytes
e5d857d
 
 
26e5cc9
e5d857d
6cd1a00
 
 
 
 
 
 
e5d857d
 
6cd1a00
00ea9d2
561ec65
 
6cd1a00
 
00ea9d2
e5d857d
 
 
26e5cc9
e5d857d
 
 
 
 
26e5cc9
6cd1a00
 
e5d857d
 
 
 
 
 
 
 
00ea9d2
33f9631
 
 
e5d857d
9e927f0
33f9631
 
 
 
 
e5d857d
 
 
 
5bd8e60
e5d857d
 
 
 
 
 
 
 
 
5bd8e60
e5d857d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import numpy as np
import matplotlib.pyplot as plt
import gradio as gr
import os  

# Function to convert HEX color to RGB tuple
def hex_to_rgb(hex_color):
    """Convert HEX color (#RRGGBB) to Matplotlib RGB tuple (R, G, B)."""
    hex_color = hex_color.lstrip("#")  # Remove #
    rgb = tuple(int(hex_color[i:i+2], 16)/255 for i in (0, 2, 4))  # Convert to (R, G, B)
    return rgb  # Matplotlib expects RGB (not HEX)

def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
    try:
        # Ensure color is valid; fallback to black if empty
        if not color or not color.startswith("#") or len(color) != 7:
            color = "#000000"  # Default to black

        # Convert HEX to RGB
        color_rgb = hex_to_rgb(color)

        x_values = np.linspace(x_min, x_max, resolution)
        functions = func_str.split(",")

        plt.figure(figsize=(6, 4), dpi=300)  

        for func_text in functions:
            func_text = func_text.strip()
            func = lambda x: eval(func_text, {"x": x, "np": np})
            y_values = func(x_values)

            # Use RGB color for Matplotlib
            plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=color_rgb, linestyle=linestyle)

        plt.xlabel("x")
        plt.ylabel("f(x)")
        plt.title("Function Plot")
        plt.legend()
        if grid:
            plt.grid()

        # Save the plot and return absolute path
        plot_filename = "high_res_plot.png"
        abs_path = os.path.abspath(plot_filename)  # Convert to absolute path
        plt.savefig(abs_path, dpi=300)
        plt.close()

        # Ensure the file exists before returning
        if os.path.exists(abs_path):
            return abs_path, abs_path
        else:
            return "Error: Plot file was not created", None

    except Exception as e:
        return f"Error: {e}", None

# Gradio Interface
with gr.Blocks() as demo:
    gr.Markdown("# Interactive Function Plotter 📈")

    with gr.Row():
        with gr.Column():
            func_str = gr.Textbox(label="Function (e.g., x**2, np.sin(x), np.exp(-x))")
            x_min = gr.Number(label="X Min", value=-10)
            x_max = gr.Number(label="X Max", value=10)
            resolution = gr.Slider(10, 1000, step=10, label="Resolution", value=100)
            color = gr.ColorPicker(label="Line Color", value="#000000")  # Default to black
            linestyle = gr.Dropdown(["solid", "dashed", "dotted", "dashdot"], label="Line Style")
            grid = gr.Checkbox(label="Show Grid", value=True)
            submit_button = gr.Button("Plot Function")

        with gr.Column():
            output_image = gr.Image(label="Function Plot")
            download_button = gr.File(label="Download High-Res Plot")

    submit_button.click(
        plot_function, 
        inputs=[func_str, x_min, x_max, resolution, color, linestyle, grid], 
        outputs=[output_image, download_button]
    )

demo.launch()