Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plot function
#2
Nope, nope, nope, nope. Your code does not run. You have the same functions defined multiple times. The code is messy and far longer than I want to read. You don't even ask a clear question. If you are asking for assistance, you really need to do better. Post a small, runnable example that demonstrates the problem you are trying to solve, a problem that is clearly described in your post.

I modified the update_plot() function so you can at least get an idea of what your program looks like with the plot appearing right of the form.
def update_plot():
    plt.clf()  # Clear the previous plot
     
    models = [model_vars[col].get() for col in range(num_columns)]
    cycle_times = [cycle_time_labels[col]['text'] for col in range(num_columns)]
     
    plt.barh(models, [float(random.randint(1, 100)) for time in cycle_times])
    plt.xlabel("Cycle Time (hours)")
    plt.ylabel("Model")
    plt.title("Cycle Time Comparison")
The existing program crashed because you called update_plot() when the form was empty. It crashes whenever you change something in the form, because editing the form calls update_plot(), and the form is only partially complete. One way to get around this is catch the exceptions in update_plot. If there is an exception, don't change the plot. Maybe display a message to let the user know why the plot isn't updating.
def update_plot():
    plt.clf()  # Clear the previous plot
     
    models = [model_vars[col].get() for col in range(num_columns)]
    cycle_times = [cycle_time_labels[col]['text'] for col in range(num_columns)]
    try:
        cycle_times = [float(time.split(":")[0]) + float(time.split(":")[1])/60 + float(time.split(":")[2])/3600 for time in cycle_times]
    except (ValueError, IndexError):
        # Maybe display message?
        return

    plt.barh(models, cycle_times)
    plt.xlabel("Cycle Time (hours)")
    plt.ylabel("Model")
    plt.title("Cycle Time Comparison")
     
    # Draw the plot onto the canvas
    canvas = FigureCanvasTkAgg(plt.gcf(), master=canvas_frame)
    canvas.draw()
    canvas.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
calculate_travel_distance() has an error. You pass a list as the travel_distance argument, but you treat travel_distance like it is a float. Not sure what the fix is because I don't know what you are trying to accomplish there. Either you need to modify the function to take a list, or you need to pass a float. I modified the function call.
def update_model_travel_times(col):
    raw_distance = travel_distance_entries[col].get()
    load_time_empty = not load_time_entries[col].get().strip()
    dump_time_empty = not dump_time_entries[col].get().strip()
    delay_time_empty = not delay_time_entries[col].get().strip()
 
    if not raw_distance.strip():
        if load_time_empty and dump_time_empty and delay_time_empty:
            model_travel_time_labels[col].config(text="Input Required")
            flash_red(model_travel_time_labels[col])
        else:
            model_travel_time_labels[col].config(text="")
            # Reset the color to the original color if it was flashed
            model_travel_time_labels[col].config(foreground="black")
    else:
        try:
            travel_distance = float(raw_distance)
            travel_time = calculate_travel_time(travel_distance[col])  # <-- Pass float, not list
            formatted_travel_time = format_duration(travel_time * 60)
            model_travel_time_labels[col].config(text=formatted_travel_time)
        except ValueError:
            model_travel_time_labels[col].config(text="Invalid Distance")
            flash_red(model_travel_time_labels[col])
Reply


Messages In This Thread
Plot function - by Sedos101 - Aug-23-2023, 09:17 AM
RE: Plot function - by deanhystad - Aug-23-2023, 01:42 PM
RE: Plot function - by Sedos101 - Aug-23-2023, 01:51 PM
RE: Plot function - by deanhystad - Aug-23-2023, 02:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to draw the Probability Density Function (PDF) plot regardless of sampe size? amylopectin 3 3,686 Mar-02-2022, 09:34 PM
Last Post: Larz60+
  How to plot intraday data of several days in one plot mistermister 3 2,994 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  How to plot vertically stacked plot with same x-axis and SriMekala 0 1,968 Jun-12-2019, 03:31 PM
Last Post: SriMekala

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020