Fixed line color not changing issue
Browse files
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
|
11 |
-
|
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=
|
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)")
|