Fixed line color issue (passing HEX color directly)
Browse files1234545679108910111314151213141915161718192021222324253 unmodified lines29303134323334import numpy as npimport matplotlib.pyplot as pltimport gradio as grimport matplotlib.colors as mcolorsimport os # For handling file pathsimport os
def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid): try: # Ensure the color is always a valid HEX string if not color or not color.startswith("#") or len(color) != 7: # Ensure color is valid (fallback to black if empty) if not color: color = "#000000" # Default to black
# Convert to proper HEX format for Matplotlib color = mcolors.to_hex(color)
x_values = np.linspace(x_min, x_max, resolution) functions = func_str.split(",")
plt.figure(figsize=(6, 4), dpi=300) # High-resolution image 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 HEX color directly without conversion plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=color, linestyle=linestyle)
plt.xlabel("x")3 unmodified lines if grid: plt.grid()
# Save the plot and return absolute path plot_filename = os.path.abspath("high_res_plot.png") plt.savefig(plot_filename, dpi=300) plt.close()app.py+6-9