xoarissa commited on
Commit
5bd8e60
·
1 Parent(s): f8918f6

Optimized and fixed invalid RGBA issue

Browse files
Files changed (1) hide show
  1. app.py +11 -20
app.py CHANGED
@@ -2,17 +2,14 @@ import numpy as np
2
  import matplotlib.pyplot as plt
3
  import gradio as gr
4
  import matplotlib.colors as mcolors
5
- import os # Import os to handle file paths
6
-
7
- def hex_to_rgb(hex_color):
8
- """Convert HEX color (e.g., '#ff5733') to an RGB tuple for Matplotlib."""
9
- if hex_color is None: # If no color is selected, default to black
10
- hex_color = "#000000"
11
- rgb = mcolors.hex2color(hex_color) # Convert HEX to RGB
12
- return rgb
13
 
14
  def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
15
  try:
 
 
 
 
16
  x_values = np.linspace(x_min, x_max, resolution)
17
  functions = func_str.split(",")
18
 
@@ -22,32 +19,26 @@ def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
22
  func_text = func_text.strip()
23
  func = lambda x: eval(func_text, {"x": x, "np": np})
24
  y_values = func(x_values)
25
-
26
- # Ensure a valid color is provided, defaulting to black
27
- rgb_color = hex_to_rgb(color)
28
-
29
  plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=rgb_color, linestyle=linestyle)
30
 
31
  plt.xlabel("x")
32
  plt.ylabel("f(x)")
33
  plt.title("Function Plot")
34
  plt.legend()
35
-
36
  if grid:
37
  plt.grid()
38
 
39
- # Save the plot as an absolute path
40
- plot_filename = "high_res_plot.png"
41
- abs_path = os.path.abspath(plot_filename) # Convert to absolute path
42
- plt.savefig(abs_path, dpi=300)
43
  plt.close()
44
 
45
- return abs_path, abs_path # Return the absolute path for Gradio to use
46
 
47
  except Exception as e:
48
  return f"Error: {e}", None
49
 
50
- # Using gr.Blocks() for better UI layout
51
  with gr.Blocks() as demo:
52
  gr.Markdown("# Interactive Function Plotter 📈")
53
 
@@ -57,7 +48,7 @@ with gr.Blocks() as demo:
57
  x_min = gr.Number(label="X Min", value=-10)
58
  x_max = gr.Number(label="X Max", value=10)
59
  resolution = gr.Slider(10, 1000, step=10, label="Resolution", value=100)
60
- color = gr.ColorPicker(label="Line Color", value="#000000") # Set default to black
61
  linestyle = gr.Dropdown(["solid", "dashed", "dotted", "dashdot"], label="Line Style")
62
  grid = gr.Checkbox(label="Show Grid", value=True)
63
  submit_button = gr.Button("Plot Function")
 
2
  import matplotlib.pyplot as plt
3
  import gradio as gr
4
  import matplotlib.colors as mcolors
5
+ import os
 
 
 
 
 
 
 
6
 
7
  def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
8
  try:
9
+ # Ensure a valid color (default to black if None)
10
+ color = color if color else "#000000"
11
+ rgb_color = mcolors.hex2color(color) # Convert HEX to RGB
12
+
13
  x_values = np.linspace(x_min, x_max, resolution)
14
  functions = func_str.split(",")
15
 
 
19
  func_text = func_text.strip()
20
  func = lambda x: eval(func_text, {"x": x, "np": np})
21
  y_values = func(x_values)
 
 
 
 
22
  plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=rgb_color, linestyle=linestyle)
23
 
24
  plt.xlabel("x")
25
  plt.ylabel("f(x)")
26
  plt.title("Function Plot")
27
  plt.legend()
 
28
  if grid:
29
  plt.grid()
30
 
31
+ # Save the plot and return absolute path
32
+ plot_filename = os.path.abspath("high_res_plot.png")
33
+ plt.savefig(plot_filename, dpi=300)
 
34
  plt.close()
35
 
36
+ return plot_filename, plot_filename
37
 
38
  except Exception as e:
39
  return f"Error: {e}", None
40
 
41
+ # Gradio Interface
42
  with gr.Blocks() as demo:
43
  gr.Markdown("# Interactive Function Plotter 📈")
44
 
 
48
  x_min = gr.Number(label="X Min", value=-10)
49
  x_max = gr.Number(label="X Max", value=10)
50
  resolution = gr.Slider(10, 1000, step=10, label="Resolution", value=100)
51
+ color = gr.ColorPicker(label="Line Color", value="#000000") # Default to black
52
  linestyle = gr.Dropdown(["solid", "dashed", "dotted", "dashdot"], label="Line Style")
53
  grid = gr.Checkbox(label="Show Grid", value=True)
54
  submit_button = gr.Button("Plot Function")