Fixed line color issue (passing HEX color directly)
Browse files1234567891011121314151617181920212223242526273 unmodified lines31323334353637import numpy as npimport matplotlib.pyplot as pltimport gradio as grimport matplotlib.colors as mcolorsimport os # For handling file pathsdef plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):try:# Ensure the color is always a valid HEX stringif not color or not color.startswith("#") or len(color) != 7:color = "#000000" # Default to black# Convert to proper HEX format for Matplotlibcolor = 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 imagefor func_text in functions:func_text = func_text.strip()func = lambda x: eval(func_text, {"x": x, "np": np})y_values = func(x_values)plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=color, linestyle=linestyle)plt.xlabel("x")3 unmodified linesif grid:plt.grid()# Save the plot and return absolute pathplot_filename = os.path.abspath("high_res_plot.png")plt.savefig(plot_filename, dpi=300)plt.close()123456789101112131415161718192021222324253 unmodified lines293031323334import numpy as npimport matplotlib.pyplot as pltimport gradio as grimport osdef plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):try:# Ensure color is valid (fallback to black if empty)if not color:color = "#000000" # Default to blackx_values = np.linspace(x_min, x_max, resolution)functions = func_str.split(",")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 conversionplt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=color, linestyle=linestyle)plt.xlabel("x")3 unmodified linesif grid:plt.grid()plot_filename = os.path.abspath("high_res_plot.png")plt.savefig(plot_filename, dpi=300)plt.close()
app.py+6-9