Beispiel #1
0
    def graphPlot(graphFrame):
        from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
        from matplotlib.figure import Figure
        from mpl_toolkits.mplot3d import Axes3D
        import matplotlib.pyplot as plt
        import numpy as np
        from itertools import product, combinations
        import matplotlib.backends.backend_tkagg as tkagg

        global canvas
        fig = plt.figure()
        ax = fig.gca(projection='3d')
        ax.set_aspect("equal")

        cubeWidth = 15

        # draw cube
        r = [0, cubeWidth]
        for s, e in combinations(np.array(list(product(r, r, r))), 2):
            if np.sum(np.abs(s - e)) == r[1] - r[0]:
                ax.plot3D(*zip(s, e), color="b")

    # draw a point
        ax.scatter([cubeWidth / 2], [cubeWidth / 2], [cubeWidth / 2],
                   color="r",
                   s=3)

        canvas = FigureCanvasTkAgg(fig, master=graphFrame)
        canvas.show()
        canvas.get_tk_widget().pack(side='top', fill='both', expand=1)

        tkagg.NavigationToolbar2TkAgg(canvas, graphFrame)
        ax.mouse_init()
Beispiel #2
0
    def __init__(self,
                 name=None,
                 figsize=(6, 4),
                 position=(0.1, 0.1, 0.8, 0.8)):
        import matplotlib.backends.backend_tkagg as mpTkAgg
        import matplotlib.figure as mpfig
        import sys

        # Window widget
        self.window = Tk.Toplevel()
        if name is not None:
            assert type(name) is str
            self.window.title(name)
        self.name = self.window.title()

        def callback():
            self.window.withdraw()

        self.window.bind("<Destroy>", callback)
        self.window.protocol("WM_DELETE_WINDOW", callback)

        button = Tk.Button(ROOT,
                           text='Show %s window' % self.window.title(),
                           command=self.window.deiconify)
        button.pack(side=Tk.TOP)
        button.pack(fill=Tk.BOTH, expand=1)

        # Instantiate figure and plot
        self.f = mpfig.Figure(figsize=figsize, dpi=100)
        self.ax = self.f.add_subplot(111)
        self.ax.set_position(position)

        # Instantiate canvas
        self.canvas = canvas = mpTkAgg.FigureCanvasTkAgg(self.f, self.window)

        # Pack canvas into window
        canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
        canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

        # Instantiate and pack toolbar
        self.toolbar = toolbar = mpTkAgg.NavigationToolbar2TkAgg(
            canvas, self.window)

        # Instantiate and pack quit button
        #self.button = button = Tk.Button(self.window, text='Quit', command=sys.exit)
        #button.pack(side=Tk.BOTTOM)

        # Show canvas and toolbar
        toolbar.update()
        canvas.show()

        # Init nplots to zero
        self.nplots = 0

        # Init names
        self.names = []
        return
    def analysis_plots_subframe(self, master):
        plotter_frame = LabelFrame(master, text="Analysis Plots")
        plotter_frame.pack(fill="both")

        button1 = Button(plotter_frame, text="Print Plot", command=self.print_plots)
        button1.pack(anchor='n', side='top', padx=10, pady=5)

        plots_frame = Frame(plotter_frame)
        plots_frame.pack(fill="both", expand=True)


        plot1_frame = Frame(plots_frame)
        # plot1_frame.pack(fill="both", expand=True, anchor='e', side='top')
        plot1_frame.grid(row=0, column=0, sticky="nw")

        plot2_frame = Frame(plots_frame)
        plot2_frame.grid(row=0, column=1, sticky="ne")
        # plot2_frame.pack(fill="both", expand=True, anchor='w', side='top')


        # initialize plot1
        self.f1 = Figure(figsize=(4, 4), dpi=100)
        self.a1 = self.f1.add_subplot(111)
        self.a1.plot([1, 2, 3, 4, 5, 6, 7, 8], [5, 6, 1, 3, 8, 9, 3, 5], 'g')

        self.canvas1 = FigureCanvasTkAgg(self.f1, plot1_frame)
        tkagg.NavigationToolbar2TkAgg(self.canvas1, plot1_frame)
        self.canvas1.show()
        self.canvas1.get_tk_widget().pack(side=BOTTOM, fill=BOTH, expand=True)

        # initialize plot2
        self.f2 = Figure(figsize=(4, 4), dpi=100)
        self.a2 = self.f2.add_subplot(111)
        self.a2.plot([1, 2, 4, 5, 8, 6, 7, 8], [5, 6, 1, 3, 8, 9, 3, 5], 'b')

        self.canvas2 = FigureCanvasTkAgg(self.f2, plot2_frame)
        tkagg.NavigationToolbar2TkAgg(self.canvas2, plot2_frame)
        self.canvas2.show()
        self.canvas2.get_tk_widget().pack(side=BOTTOM, fill=BOTH, expand=True)
Beispiel #4
0
    def plot(graphFrame):
        from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
        import matplotlib.backends.backend_tkagg as tkagg
        from matplotlib.figure import Figure

        fig = Figure()
        ax = fig.add_subplot(111)
        line, = ax.plot(range(10))

        canvas = FigureCanvasTkAgg(fig, master=graphFrame)
        canvas.show()
        canvas.get_tk_widget().pack(side='top', fill='both', expand=1)

        # canvas is your canvas, and root is your parent (Frame, TopLevel, Tk instance etc.)
        tkagg.NavigationToolbar2TkAgg(canvas, graphFrame)
Beispiel #5
0
    def plot(self):
        canvas = tkagg.FigureCanvasTkAgg(self.figure, master=self.window)
        wcanvas = canvas.get_tk_widget()
        wcanvas.config(width=1000, height=300)
        wcanvas.pack(side=tk.TOP, expand=True, fill=tk.BOTH)

        toolbar = tkagg.NavigationToolbar2TkAgg(canvas, self.window)
        toolbar.update()
        self.output = tk.StringVar()
        label = tk.Label(self.window, textvariable=self.output, bg="white", fg="red", bd=2)
        label.pack(side=tk.RIGHT, expand=True, fill=tk.X)
        wcanvas.pack(side=tk.TOP, expand=True, fill=tk.BOTH)

        canvas.draw()

        # Print task type using mouse clicks or motion.
        if self.motion:
            fig.canvas.mpl_connect('motion_notify_event', self.onclick)
        else:
            fig.canvas.mpl_connect('button_press_event', self.onclick)
Beispiel #6
0
def add_canvas(figure, container):
    """Add the figure and toolbar to GUI.

    Args:
        figure (matplotlib.Figure): The figure object to visualize.
        container: The GUI part where the figure is shown.
    """
    canvas = tk_backend.FigureCanvasTkAgg(figure, master=container)
    canvas.draw()
    canvas.get_tk_widget().pack(side=tkinter.TOP,
                                fill=tkinter.BOTH,
                                expand=True)

    # NavigationToolbar2TkAgg is deprecated since matplotlib 2.2,
    # NavigationToolbar2Tk should be used and is only available in new version.
    toolbar = tk_backend.NavigationToolbar2Tk(canvas, container) \
            if hasattr(tk_backend, 'NavigationToolbar2Tk') \
            else tk_backend.NavigationToolbar2TkAgg(canvas, container)
    toolbar.update()
    canvas._tkcanvas.pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=True)

    pyplot.close(figure.number)
Beispiel #7
0
executebutton.pack(side=BOTTOM)

###########

dataframe = Frame(root)
dataframe.pack(side=LEFT)

fig = plt.figure(1)
plt.title('Estimated impulse response')
plt.ylabel('Amplitude')
plt.xlabel('Time [us]')

canvas = tkagg.FigureCanvasTkAgg(fig, master=dataframe)
plot_widget = canvas.get_tk_widget()
plot_widget.pack(side=TOP)
toolbar = tkagg.NavigationToolbar2TkAgg(canvas, dataframe)
toolbar.pack(side=TOP)

######################
saveframe = Frame(dataframe)
saveframe.pack(side=BOTTOM)
lSaveFrame = Frame(saveframe)
lSaveFrame.pack(side=LEFT)
eSaveFrame = Frame(saveframe)
eSaveFrame.pack(side=LEFT)
sSaveFrame = Frame(saveframe)
sSaveFrame.pack(side=LEFT)

labelSave = Label(lSaveFrame, text="File name:")
labelSave.pack(side=TOP)
Beispiel #8
0
def create_graph_frame(master, *args, func_name, var_name, func, h, a,
                       **kwargs):
    root = Frame(master, *args, **kwargs)

    figure = plt.Figure()
    canvas = tkagg.FigureCanvasTkAgg(master=root, figure=figure)
    axes = figure.add_subplot(1, 1, 1)
    axes.set_xlabel(var_name)
    axes.set_ylabel(func_name)

    toolbar_frame = Frame(root)
    tkagg.NavigationToolbar2TkAgg(canvas=canvas, window=toolbar_frame)

    sections_entry = PositiveNumEntry(root)
    plot_button = Button(root, text='plot')

    canvas.get_tk_widget().pack(fill='both', expand=True)
    toolbar_frame.pack(fill='x')

    plot_button.pack(side='bottom')

    Label(root, text='Number of sections:').pack(side='left',
                                                 expand=True,
                                                 anchor='e')
    sections_entry.pack(side='right', expand=True, anchor='w')

    if var_name == 'x':
        endpoint, fixed_var_endpoint = h, a
        fixed_var_name = 'y'
    else:
        endpoint, fixed_var_endpoint = a, h
        fixed_var_name = 'x'

    timer = Timer()

    plot_dots = linspace(0, endpoint, 200)

    # TODO:
    # 1) don't use fixed_var_name
    # 2) set_xlabel and ylabel: maybe you can remember it out of this function?
    # 3) last_used_values: how to make it simpler?
    last_used_values = dict()

    def butt_command():
        axes.cla()
        axes.set_xlabel(var_name)
        axes.set_ylabel(func_name)

        print("Plotting graph...")
        timer.start()

        sections_num = int(sections_entry.get())
        fixed_var_vals = linspace(0, fixed_var_endpoint, sections_num + 2)
        labels = create_lines_labels(var_name, sections_num)
        values = dict()
        for label, fixed_var in zip(labels, fixed_var_vals):
            if fixed_var in last_used_values:
                values[fixed_var] = last_used_values[fixed_var]
            else:
                values[fixed_var] = [
                    func(**{
                        var_name: var,
                        fixed_var_name: fixed_var
                    }) for var in plot_dots
                ]
            axes.plot(plot_dots, values[fixed_var], label=label)

        axes.legend(loc='best').draggable()

        canvas.draw()
        print(f'(done in {timer})')

        last_used_values.clear()
        last_used_values.update(values)

    plot_button.config(command=butt_command)

    return root
Beispiel #9
0
    def plot_cuboid(graphFrame, center, size):
        from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
        from matplotlib.figure import Figure
        from mpl_toolkits.mplot3d import Axes3D
        import matplotlib.pyplot as plt
        import numpy as np
        from itertools import product, combinations

        global canvas
        ox, oy, oz = center
        l, w, h = size

        x = np.linspace(ox - l / 2, ox + l / 2, num=10)
        y = np.linspace(oy - w / 2, oy + w / 2, num=10)
        z = np.linspace(oz - h / 2, oz + h / 2, num=10)
        x1, z1 = np.meshgrid(x, z)
        y11 = np.ones_like(x1) * (oy - w / 2)
        y12 = np.ones_like(x1) * (oy + w / 2)
        x2, y2 = np.meshgrid(x, y)
        z21 = np.ones_like(x2) * (oz - h / 2)
        z22 = np.ones_like(x2) * (oz + h / 2)
        y3, z3 = np.meshgrid(y, z)
        x31 = np.ones_like(y3) * (ox - l / 2)
        x32 = np.ones_like(y3) * (ox + l / 2)

        from mpl_toolkits.mplot3d import Axes3D
        import matplotlib.pyplot as plt
        fig = plt.figure()
        ax = fig.gca(projection='3d')
        ax.set_aspect("equal")
        ax.autoscale(
            enable=False,
            axis='both')  #you will need this line to change the Z-axis
        ax.set_xbound(0 - 200, 0 + 200)
        ax.set_ybound(0 - 200, 0 + 200)
        ax.set_zbound(0 - 200, 0 + 200)
        # outside surface
        ax.plot_wireframe(x1,
                          y11,
                          z1,
                          color='b',
                          rstride=10,
                          cstride=10,
                          alpha=0.6)
        # inside surface
        ax.plot_wireframe(x1,
                          y12,
                          z1,
                          color='b',
                          rstride=10,
                          cstride=10,
                          alpha=0.6)
        # bottom surface
        ax.plot_wireframe(x2,
                          y2,
                          z21,
                          color='b',
                          rstride=10,
                          cstride=10,
                          alpha=0.6)
        # upper surface
        ax.plot_wireframe(x2,
                          y2,
                          z22,
                          color='b',
                          rstride=10,
                          cstride=10,
                          alpha=0.6)
        # left surface
        ax.plot_wireframe(x31,
                          y3,
                          z3,
                          color='b',
                          rstride=10,
                          cstride=10,
                          alpha=0.6)
        # right surface
        ax.plot_wireframe(x32,
                          y3,
                          z3,
                          color='b',
                          rstride=10,
                          cstride=10,
                          alpha=0.6)
        ax.set_xlabel('X')
        ax.set_xlim(-10, 10)
        ax.set_ylabel('Y')
        ax.set_ylim(-10, 10)
        ax.set_zlabel('Z')
        ax.set_zlim(-10, 10)

        # draw a point
        ax.scatter([0], [0], [0], color="r", s=3)

        canvas = FigureCanvasTkAgg(fig, master=graphFrame)
        canvas.show()
        canvas.get_tk_widget().pack(side='top', fill='both', expand=1)

        import matplotlib.backends.backend_tkagg as tkagg
        # canvas is your canvas, and root is your parent (Frame, TopLevel, Tk instance etc.)
        tkagg.NavigationToolbar2TkAgg(canvas, graphFrame)
        ax.mouse_init()