xoarissa commited on
Commit
26e5cc9
·
1 Parent(s): a416c13

Fixed line color issue (passing HEX color directly)

Browse files
Files changed (1) hide show
  1. app.py +6 -9
app.py CHANGED
@@ -1,27 +1,25 @@
1
  import numpy as np
2
  import matplotlib.pyplot as plt
3
  import gradio as gr
4
- import matplotlib.colors as mcolors
5
- import os # For handling file paths
6
 
7
  def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
8
  try:
9
- # Ensure the color is always a valid HEX string
10
- if not color or not color.startswith("#") or len(color) != 7:
11
  color = "#000000" # Default to black
12
 
13
- # Convert to proper HEX format for Matplotlib
14
- color = mcolors.to_hex(color)
15
-
16
  x_values = np.linspace(x_min, x_max, resolution)
17
  functions = func_str.split(",")
18
 
19
- plt.figure(figsize=(6, 4), dpi=300) # High-resolution image
20
 
21
  for func_text in functions:
22
  func_text = func_text.strip()
23
  func = lambda x: eval(func_text, {"x": x, "np": np})
24
  y_values = func(x_values)
 
 
25
  plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=color, linestyle=linestyle)
26
 
27
  plt.xlabel("x")
@@ -31,7 +29,6 @@ def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
31
  if grid:
32
  plt.grid()
33
 
34
- # Save the plot and return absolute path
35
  plot_filename = os.path.abspath("high_res_plot.png")
36
  plt.savefig(plot_filename, dpi=300)
37
  plt.close()
 
1
  import numpy as np
2
  import matplotlib.pyplot as plt
3
  import gradio as gr
4
+ import os
 
5
 
6
  def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
7
  try:
8
+ # Ensure color is valid (fallback to black if empty)
9
+ if not color:
10
  color = "#000000" # Default to black
11
 
 
 
 
12
  x_values = np.linspace(x_min, x_max, resolution)
13
  functions = func_str.split(",")
14
 
15
+ plt.figure(figsize=(6, 4), dpi=300)
16
 
17
  for func_text in functions:
18
  func_text = func_text.strip()
19
  func = lambda x: eval(func_text, {"x": x, "np": np})
20
  y_values = func(x_values)
21
+
22
+ # Use HEX color directly without conversion
23
  plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=color, linestyle=linestyle)
24
 
25
  plt.xlabel("x")
 
29
  if grid:
30
  plt.grid()
31
 
 
32
  plot_filename = os.path.abspath("high_res_plot.png")
33
  plt.savefig(plot_filename, dpi=300)
34
  plt.close()