Update to use the new Matplotlib interface
This commit is contained in:
		
							parent
							
								
									a3753b25bf
								
							
						
					
					
						commit
						3f899f624b
					
				
					 2 changed files with 19 additions and 47 deletions
				
			
		|  | @ -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() | ||||
|  |  | |||
|  | @ -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() | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue