Python Forum

Full Version: Showing an empty chart, then input data via function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.

I have been able to create a chart with the data I am looking for via button click/function. The chart only shows after I click on the button to run a function. I am hoping to have an empty chart sitting in my widget, then once the function is called the chart data is placed on the chart. Below is the code I am using.


Thanks for your suggestions

class Page3(tk.Frame):
    def __init__(self, master, **kw):
        super().__init__(master, **kw)
        self.config(background='black')

        def graph_data():

            last_name = n_entry.get()
            #Connect to sqlite and grab the data for the last_name variable above not shown...


            categories = ['Sleep', 'Fatigue', 'Soreness', 'Stress', 'Mood', 'Diet', 'RPE', 'Sleep']
            categories2 = ['Sleep', 'Fatigue', 'Soreness', 'Stress', 'Mood', 'Diet', 'RPE']
            lbls = [0., 0.8975979,  1.7951958,  2.6927937,  3.5903916,  4.48798951, 5.38558741]
            graph = [sle, fat, sor, ste, moo, die, rpe, sle]
            others = [t_sle, t_fat, t_sor, t_ste, t_moo, t_die, t_rpe, t_sle]

            label_loc = np.linspace(start=0, stop=2 * np.pi, num=len(categories))

            fig = plt.figure(figsize=(10, 9), dpi=40, facecolor='#2E2E2E')
            ax = plt.subplot(polar=True)
            plt.plot(label_loc, graph, label='Name', color='#3769b9')
            plt.fill(label_loc, others, color='#D23C1E', alpha=.25)
            plt.plot(label_loc, others, label='team', color='#D23C1E')
            plt.title('Metrics', size=40, color='white')
            ax.set_yticklabels([])
            ax.set_ylim(0, max(graph) +.1)
            ax.set_facecolor('#2E2E2E')
            plt.legend(loc='upper left')
            plt.subplots_adjust(left=0.05, bottom=0.05, right=0.92, top=0.90)
            plt.thetagrids(np.degrees(lbls), categories2, size=20, color='white')
            for label, angle in zip(ax.get_xticklabels(), label_loc):
                if.1 < angle < 1:
                    label.set_horizontalalignment('left')
                    label.set_verticalalignment('bottom')
                elif 1.1 < angle < 2:
                    label.set_horizontalalignment('right')
                    label.set_verticalalignment('bottom')
                elif 2.1 < angle < 3:
                    label.set_horizontalalignment('right')
                    label.set_verticalalignment('bottom')
                elif 3.1 < angle < 4:
                    label.set_horizontalalignment('right')
                    label.set_verticalalignment('top')
                elif 4.1 < angle < 5:
                    label.set_horizontalalignment('right')
                    label.set_verticalalignment('top')
                elif 4.1 < angle < 5:
                    label.set_horizontalalignment('right')
                    label.set_verticalalignment('top')
                elif 4.1 < angle < 5:
                    label.set_horizontalalignment('left')
                    label.set_verticalalignment('top')
                else:
                    label.set_horizontalalignment('left')

            canvas = FigureCanvasTkAgg(fig, master=graph_frame)
            canvas.get_tk_widget().pack(anchor=E)
            canvas.draw()

        button_frame = LabelFrame(self, background='#2E2E2E')
        button_frame.pack(fill='x', expand=YES, padx=20, pady=20, anchor='n')

        n_label = Label(button_frame, text="Name", background='#2E2E2E')
        n_label.grid(row=0, column=1, padx=10, pady=10)
        n_entry = Entry(button_frame)
        n_entry.grid(row=0, column=2, pady=10)
        
        graph_frame = LabelFrame(self, background='#2E2E2E')
        graph_frame.pack(fill='x', expand=YES, padx=20, pady=10, anchor=S)

        g_button = Button(graph_frame, text="show graph", command=graph_data)
        g_button.pack()