From 3f899f624b2572d3e89d7d1f73e8186e63f4cbc7 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Mon, 25 Nov 2019 01:04:49 -0500 Subject: [PATCH] Update to use the new Matplotlib interface --- DemoPrograms/Demo_Pyplot_Bar_Chart.py | 35 ++++++++------------------ DemoPrograms/Demo_Pyploy_Bar_Chart2.py | 31 +++++++---------------- 2 files changed, 19 insertions(+), 47 deletions(-) diff --git a/DemoPrograms/Demo_Pyplot_Bar_Chart.py b/DemoPrograms/Demo_Pyplot_Bar_Chart.py index efade3dd..b8822758 100644 --- a/DemoPrograms/Demo_Pyplot_Bar_Chart.py +++ b/DemoPrograms/Demo_Pyplot_Bar_Chart.py @@ -1,11 +1,10 @@ #!/usr/bin/env python import PySimpleGUI as sg import matplotlib -matplotlib.use('TkAgg') -from matplotlib.backends.backend_tkagg import FigureCanvasAgg -import matplotlib.backends.tkagg as tkagg -import tkinter as Tk - +from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg +# matplotlib.use('TkAgg') +import numpy as np +import matplotlib.pyplot as plt """ Demonstrates one way of embedding Matplotlib figures into a PySimpleGUI window. @@ -21,9 +20,6 @@ If you want to change the GUI, make changes to the GUI portion marked below. # ------------------------------- PASTE YOUR MATPLOTLIB CODE HERE ------------------------------- -import numpy as np -import matplotlib.pyplot as plt - values_to_plot = (20, 35, 30, 35, 27) ind = np.arange(len(values_to_plot)) width = 0.4 @@ -41,32 +37,21 @@ plt.legend((p1[0],), ('Data Group 1',)) # ------------------------------- Beginning of Matplotlib helper code ----------------------- - def draw_figure(canvas, figure, loc=(0, 0)): - """ Draw a matplotlib figure onto a Tk canvas - - loc: location of top-left corner of figure on canvas in pixels. - - Inspired by matplotlib source: lib/matplotlib/backends/backend_tkagg.py - """ - figure_canvas_agg = FigureCanvasAgg(figure) + figure_canvas_agg = FigureCanvasTkAgg(figure, canvas) figure_canvas_agg.draw() - figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds - figure_w, figure_h = int(figure_w), int(figure_h) - photo = Tk.PhotoImage(master=canvas, width=figure_w, height=figure_h) - canvas.create_image(loc[0] + figure_w / 2, loc[1] + figure_h / 2, image=photo) - tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2) - return photo - + figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1) + return figure_canvas_agg # ------------------------------- Beginning of GUI CODE ------------------------------- +sg.change_look_and_feel('Light Brown 3') fig = plt.gcf() # if using Pyplot then get the figure from the plot figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds # define the window layout layout = [[sg.Text('Plot test', font='Any 18')], - [sg.Canvas(size=(figure_w, figure_h), key='canvas')], + [sg.Canvas(size=(figure_w, figure_h), key='-CANVAS-')], [sg.OK(pad=((figure_w / 2, 0), 3), size=(4, 2))]] # create the form and show it without the plot @@ -74,7 +59,7 @@ window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI', layout, force_toplevel=True, finalize=True) # add the plot to the window -fig_photo = draw_figure(window['canvas'].TKCanvas, fig) +fig_photo = draw_figure(window['-CANVAS-'].TKCanvas, fig) # show it all again and get buttons event, values = window.read() diff --git a/DemoPrograms/Demo_Pyploy_Bar_Chart2.py b/DemoPrograms/Demo_Pyploy_Bar_Chart2.py index 2791538f..09e86e1a 100644 --- a/DemoPrograms/Demo_Pyploy_Bar_Chart2.py +++ b/DemoPrograms/Demo_Pyploy_Bar_Chart2.py @@ -1,12 +1,9 @@ #!/usr/bin/env python +import PySimpleGUI as sg +from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg import numpy as np import matplotlib.pyplot as plt -import tkinter as Tk -import matplotlib.backends.tkagg as tkagg -from matplotlib.backends.backend_tkagg import FigureCanvasAgg -import PySimpleGUI as sg -import matplotlib -matplotlib.use('TkAgg') +# matplotlib.use('TkAgg') """ Demonstrates one way of embedding Matplotlib figures into a PySimpleGUI window. @@ -38,32 +35,22 @@ plt.title('Market Share for Each Genre 1995-2017') # ------------------------------- Beginning of Matplotlib helper code ----------------------- - def draw_figure(canvas, figure, loc=(0, 0)): - """ Draw a matplotlib figure onto a Tk canvas - - loc: location of top-left corner of figure on canvas in pixels. - - Inspired by matplotlib source: lib/matplotlib/backends/backend_tkagg.py - """ - figure_canvas_agg = FigureCanvasAgg(figure) + figure_canvas_agg = FigureCanvasTkAgg(figure, canvas) figure_canvas_agg.draw() - figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds - figure_w, figure_h = int(figure_w), int(figure_h) - photo = Tk.PhotoImage(master=canvas, width=figure_w, height=figure_h) - canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo) - tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2) - return photo + figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1) + return figure_canvas_agg # ------------------------------- Beginning of GUI CODE ------------------------------- +sg.change_look_and_feel('Light Brown 3') fig = plt.gcf() # if using Pyplot then get the figure from the plot figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds # define the window layout layout = [[sg.Text('Plot test', font='Any 18')], - [sg.Canvas(size=(figure_w, figure_h), key='canvas')], + [sg.Canvas(size=(figure_w, figure_h), key='-CANVAS-')], [sg.OK(pad=((figure_w / 2, 0), 3), size=(4, 2))]] # create the form and show it without the plot @@ -71,7 +58,7 @@ window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI', layout, force_toplevel=True, finalize=True) # add the plot to the window -fig_photo = draw_figure(window['canvas'].TKCanvas, fig) +fig_photo = draw_figure(window['-CANVAS-'].TKCanvas, fig) # show it all again and get buttons event, values = window.read()