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

Fixed line color not changing issue

Browse files
Files changed (1) hide show
  1. app.py +8 -6
app.py CHANGED
@@ -2,14 +2,16 @@ import numpy as np
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,7 +21,7 @@ def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
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)")
 
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 a valid color (default to black if None or invalid)
10
+ if not color or not color.startswith("#") or len(color) != 7:
11
+ color = "#000000" # Default to black
12
+
13
+ rgba_color = mcolors.to_rgba(color) # Convert HEX to RGBA
14
+
15
  x_values = np.linspace(x_min, x_max, resolution)
16
  functions = func_str.split(",")
17
 
 
21
  func_text = func_text.strip()
22
  func = lambda x: eval(func_text, {"x": x, "np": np})
23
  y_values = func(x_values)
24
+ plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=rgba_color, linestyle=linestyle)
25
 
26
  plt.xlabel("x")
27
  plt.ylabel("f(x)")