Merge pull request #2245 from PySimpleGUI/Dev-latest
Update to use the new Matplotlib interface
This commit is contained in:
commit
3ddb991df4
|
@ -1,11 +1,10 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import PySimpleGUI as sg
|
import PySimpleGUI as sg
|
||||||
import matplotlib
|
import matplotlib
|
||||||
matplotlib.use('TkAgg')
|
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
||||||
from matplotlib.backends.backend_tkagg import FigureCanvasAgg
|
# matplotlib.use('TkAgg')
|
||||||
import matplotlib.backends.tkagg as tkagg
|
import numpy as np
|
||||||
import tkinter as Tk
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Demonstrates one way of embedding Matplotlib figures into a PySimpleGUI window.
|
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 -------------------------------
|
# ------------------------------- PASTE YOUR MATPLOTLIB CODE HERE -------------------------------
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
|
|
||||||
values_to_plot = (20, 35, 30, 35, 27)
|
values_to_plot = (20, 35, 30, 35, 27)
|
||||||
ind = np.arange(len(values_to_plot))
|
ind = np.arange(len(values_to_plot))
|
||||||
width = 0.4
|
width = 0.4
|
||||||
|
@ -41,32 +37,21 @@ plt.legend((p1[0],), ('Data Group 1',))
|
||||||
|
|
||||||
# ------------------------------- Beginning of Matplotlib helper code -----------------------
|
# ------------------------------- Beginning of Matplotlib helper code -----------------------
|
||||||
|
|
||||||
|
|
||||||
def draw_figure(canvas, figure, loc=(0, 0)):
|
def draw_figure(canvas, figure, loc=(0, 0)):
|
||||||
""" Draw a matplotlib figure onto a Tk canvas
|
figure_canvas_agg = FigureCanvasTkAgg(figure, 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.draw()
|
figure_canvas_agg.draw()
|
||||||
figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds
|
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
|
||||||
figure_w, figure_h = int(figure_w), int(figure_h)
|
return figure_canvas_agg
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------- Beginning of GUI CODE -------------------------------
|
# ------------------------------- 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
|
fig = plt.gcf() # if using Pyplot then get the figure from the plot
|
||||||
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
|
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
|
||||||
|
|
||||||
# define the window layout
|
# define the window layout
|
||||||
layout = [[sg.Text('Plot test', font='Any 18')],
|
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))]]
|
[sg.OK(pad=((figure_w / 2, 0), 3), size=(4, 2))]]
|
||||||
|
|
||||||
# create the form and show it without the plot
|
# 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)
|
layout, force_toplevel=True, finalize=True)
|
||||||
|
|
||||||
# add the plot to the window
|
# 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
|
# show it all again and get buttons
|
||||||
event, values = window.read()
|
event, values = window.read()
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
import PySimpleGUI as sg
|
||||||
|
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import tkinter as Tk
|
# matplotlib.use('TkAgg')
|
||||||
import matplotlib.backends.tkagg as tkagg
|
|
||||||
from matplotlib.backends.backend_tkagg import FigureCanvasAgg
|
|
||||||
import PySimpleGUI as sg
|
|
||||||
import matplotlib
|
|
||||||
matplotlib.use('TkAgg')
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Demonstrates one way of embedding Matplotlib figures into a PySimpleGUI window.
|
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 -----------------------
|
# ------------------------------- Beginning of Matplotlib helper code -----------------------
|
||||||
|
|
||||||
|
|
||||||
def draw_figure(canvas, figure, loc=(0, 0)):
|
def draw_figure(canvas, figure, loc=(0, 0)):
|
||||||
""" Draw a matplotlib figure onto a Tk canvas
|
figure_canvas_agg = FigureCanvasTkAgg(figure, 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.draw()
|
figure_canvas_agg.draw()
|
||||||
figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds
|
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
|
||||||
figure_w, figure_h = int(figure_w), int(figure_h)
|
return figure_canvas_agg
|
||||||
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
|
|
||||||
|
|
||||||
# ------------------------------- Beginning of GUI CODE -------------------------------
|
# ------------------------------- 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
|
fig = plt.gcf() # if using Pyplot then get the figure from the plot
|
||||||
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
|
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
|
||||||
|
|
||||||
# define the window layout
|
# define the window layout
|
||||||
layout = [[sg.Text('Plot test', font='Any 18')],
|
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))]]
|
[sg.OK(pad=((figure_w / 2, 0), 3), size=(4, 2))]]
|
||||||
|
|
||||||
# create the form and show it without the plot
|
# 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)
|
layout, force_toplevel=True, finalize=True)
|
||||||
|
|
||||||
# add the plot to the window
|
# 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
|
# show it all again and get buttons
|
||||||
event, values = window.read()
|
event, values = window.read()
|
||||||
|
|
Loading…
Reference in New Issue