Update of ALL Matplotlib demos so they run on 3.1. Brought up to date too.
This commit is contained in:
parent
6f02fb4a54
commit
3b24d8921e
|
@ -84,7 +84,7 @@ def draw_figure(canvas, figure, loc=(0, 0)):
|
||||||
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
|
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
|
||||||
figure_canvas_agg.draw()
|
figure_canvas_agg.draw()
|
||||||
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
|
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
|
||||||
|
return figure_canvas_agg
|
||||||
#------------------------------- Beginning of GUI CODE -------------------------------
|
#------------------------------- Beginning of GUI CODE -------------------------------
|
||||||
|
|
||||||
# define the window layout
|
# define the window layout
|
||||||
|
@ -96,6 +96,6 @@ layout = [[sg.Text('Plot test', font='Any 18')],
|
||||||
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI', layout, finalize=True)
|
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI', layout, finalize=True)
|
||||||
|
|
||||||
# add the plot to the window
|
# add the plot to the window
|
||||||
fig_photo = draw_figure(window['canvas'].TKCanvas, fig)
|
fig_canvas_agg = draw_figure(window['canvas'].TKCanvas, fig)
|
||||||
|
|
||||||
event, values = window.read()
|
event, values = window.read()
|
||||||
|
|
|
@ -1,64 +1,55 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import sys
|
import PySimpleGUI as sg
|
||||||
if sys.version_info[0] >= 3:
|
|
||||||
import PySimpleGUI as sg
|
|
||||||
else:
|
|
||||||
import PySimpleGUI27 as sg
|
|
||||||
|
|
||||||
from random import randint
|
from random import randint
|
||||||
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, FigureCanvasAgg
|
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, FigureCanvasAgg
|
||||||
from matplotlib.figure import Figure
|
from matplotlib.figure import Figure
|
||||||
import matplotlib.backends.tkagg as tkagg
|
|
||||||
import tkinter as tk
|
|
||||||
|
|
||||||
|
def draw_figure(canvas, figure, loc=(0, 0)):
|
||||||
|
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
|
||||||
|
figure_canvas_agg.draw()
|
||||||
|
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
|
||||||
|
return figure_canvas_agg
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
fig = Figure()
|
|
||||||
|
|
||||||
|
NUM_DATAPOINTS = 10000
|
||||||
|
# define the form layout
|
||||||
|
layout = [[sg.Text('Animated Matplotlib', size=(40, 1), justification='center', font='Helvetica 20')],
|
||||||
|
[sg.Canvas(size=(640, 480), key='-CANVAS-')],
|
||||||
|
[sg.Text('Progress through the data')],
|
||||||
|
[sg.Slider(range=(0, NUM_DATAPOINTS), size=(60, 10), orientation='h', key='-SLIDER-')],
|
||||||
|
[sg.Text('Number of data points to display on screen')],
|
||||||
|
[sg.Slider(range=(10, 500), default_value=40, size=(40, 10), orientation='h', key='-SLIDER-DATAPOINTS-')],
|
||||||
|
[sg.Button('Exit', size=(10, 1), pad=((280, 0), 3), font='Helvetica 14')]]
|
||||||
|
|
||||||
|
# create the form and show it without the plot
|
||||||
|
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI', layout, finalize=True)
|
||||||
|
|
||||||
|
canvas_elem = window.FindElement('-CANVAS-')
|
||||||
|
slider_elem = window.FindElement('-SLIDER-')
|
||||||
|
canvas = canvas_elem.TKCanvas
|
||||||
|
|
||||||
|
# draw the initial plot in the window
|
||||||
|
fig = Figure()
|
||||||
ax = fig.add_subplot(111)
|
ax = fig.add_subplot(111)
|
||||||
ax.set_xlabel("X axis")
|
ax.set_xlabel("X axis")
|
||||||
ax.set_ylabel("Y axis")
|
ax.set_ylabel("Y axis")
|
||||||
ax.grid()
|
ax.grid()
|
||||||
|
fig_agg = draw_figure(canvas, fig)
|
||||||
|
# make a bunch of random data points
|
||||||
|
dpts = [randint(0, 10) for x in range(NUM_DATAPOINTS)]
|
||||||
|
|
||||||
# define the form layout
|
|
||||||
layout = [[sg.Text('Animated Matplotlib', size=(40, 1), justification='center', font='Helvetica 20')],
|
|
||||||
[sg.Canvas(size=(640, 480), key='canvas')],
|
|
||||||
[sg.Slider(range=(0, 10000), size=(60, 10), orientation='h', key='slider')],
|
|
||||||
[sg.ReadButton('Exit', size=(10, 2), pad=((280, 0), 3), font='Helvetica 14')]]
|
|
||||||
|
|
||||||
# create the form and show it without the plot
|
|
||||||
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI').Layout(layout).Finalize()
|
|
||||||
|
|
||||||
canvas_elem = window.FindElement('canvas')
|
|
||||||
slider_elem = window.FindElement('slider')
|
|
||||||
graph = FigureCanvasTkAgg(fig, master=canvas_elem.TKCanvas)
|
|
||||||
canvas = canvas_elem.TKCanvas
|
|
||||||
|
|
||||||
dpts = [randint(0, 10) for x in range(10000)]
|
|
||||||
for i in range(len(dpts)):
|
for i in range(len(dpts)):
|
||||||
event, values = window.Read(timeout=10)
|
event, values = window.Read(timeout=10)
|
||||||
if event in ('Exit', None):
|
if event in ('Exit', None):
|
||||||
exit(69)
|
exit(69)
|
||||||
|
slider_elem.Update(i) # slider shows "progress" through the data points
|
||||||
slider_elem.Update(i)
|
ax.cla() # clear the subplot
|
||||||
ax.cla()
|
ax.grid() # draw the grid
|
||||||
ax.grid()
|
data_points = int(values['-SLIDER-DATAPOINTS-']) # draw this many data points (on next line)
|
||||||
DATA_POINTS_PER_SCREEN = 40
|
ax.plot(range(data_points), dpts[i:i+data_points], color='purple')
|
||||||
ax.plot(range(DATA_POINTS_PER_SCREEN), dpts[i:i+DATA_POINTS_PER_SCREEN], color='purple')
|
fig_agg.draw()
|
||||||
graph.draw()
|
|
||||||
figure_x, figure_y, figure_w, figure_h = fig.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(640/2, 480/2, image=photo)
|
|
||||||
|
|
||||||
figure_canvas_agg = FigureCanvasAgg(fig)
|
|
||||||
figure_canvas_agg.draw()
|
|
||||||
|
|
||||||
# Unfortunately, there's no accessor for the pointer to the native renderer
|
|
||||||
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
|
|
||||||
|
|
||||||
# time.sleep(.1)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -1,66 +1,50 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import sys
|
|
||||||
if sys.version_info[0] >= 3:
|
|
||||||
import PySimpleGUI as sg
|
|
||||||
else:
|
|
||||||
import PySimpleGUI27 as sg
|
|
||||||
|
|
||||||
from random import randint
|
|
||||||
import PySimpleGUI as sg
|
import PySimpleGUI as sg
|
||||||
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, FigureCanvasAgg
|
|
||||||
from matplotlib.figure import Figure
|
import PySimpleGUI as sg
|
||||||
import matplotlib.backends.tkagg as tkagg
|
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
||||||
import tkinter as tk
|
import matplotlib.pyplot as plt
|
||||||
|
from numpy.random import rand
|
||||||
|
|
||||||
|
def draw_figure(canvas, figure):
|
||||||
|
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
|
||||||
|
figure_canvas_agg.draw()
|
||||||
|
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
|
||||||
|
return figure_canvas_agg
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# define the form layout
|
# define the form layout
|
||||||
layout = [[sg.Text('Animated Matplotlib', size=(40, 1), justification='center', font='Helvetica 20')],
|
layout = [[sg.Text('Animated Matplotlib', size=(40, 1), justification='center', font='Helvetica 20')],
|
||||||
[sg.Canvas(size=(640, 480), key='canvas')],
|
[sg.Canvas(size=(640, 480), key='-CANVAS-')],
|
||||||
[sg.ReadButton('Exit', size=(10, 2), pad=((280, 0), 3), font='Helvetica 14')]]
|
[sg.Button('Exit', size=(10, 2), pad=((280, 0), 3), font='Helvetica 14')]]
|
||||||
|
|
||||||
# create the form and show it without the plot
|
# create the form and show it without the plot
|
||||||
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI').Layout(layout).Finalize()
|
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI', layout, finalize=True)
|
||||||
|
|
||||||
canvas_elem = window.FindElement('canvas')
|
canvas_elem = window.FindElement('-CANVAS-')
|
||||||
canvas = canvas_elem.TKCanvas
|
canvas = canvas_elem.TKCanvas
|
||||||
|
# draw the intitial scatter plot
|
||||||
|
fig, ax = plt.subplots()
|
||||||
|
ax.grid(True)
|
||||||
|
fig_agg = draw_figure(canvas, fig)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
event, values = window.Read(timeout=10)
|
event, values = window.Read(timeout=10)
|
||||||
if event in ('Exit', None):
|
if event in ('Exit', None):
|
||||||
exit(69)
|
exit(69)
|
||||||
|
|
||||||
def PyplotScatterWithLegend():
|
ax.cla()
|
||||||
import matplotlib.pyplot as plt
|
ax.grid(True)
|
||||||
from numpy.random import rand
|
for color in ['red', 'green', 'blue']:
|
||||||
|
n = 750
|
||||||
fig, ax = plt.subplots()
|
x, y = rand(2, n)
|
||||||
for color in ['red', 'green', 'blue']:
|
scale = 200.0 * rand(n)
|
||||||
n = 750
|
ax.scatter(x, y, c=color, s=scale, label=color, alpha=0.3, edgecolors='none')
|
||||||
x, y = rand(2, n)
|
ax.legend()
|
||||||
scale = 200.0 * rand(n)
|
fig_agg.draw()
|
||||||
ax.scatter(x, y, c=color, s=scale, label=color,
|
|
||||||
alpha=0.3, edgecolors='none')
|
|
||||||
|
|
||||||
ax.legend()
|
|
||||||
ax.grid(True)
|
|
||||||
return fig
|
|
||||||
|
|
||||||
fig = PyplotScatterWithLegend()
|
|
||||||
|
|
||||||
figure_x, figure_y, figure_w, figure_h = fig.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(640/2, 480/2, image=photo)
|
|
||||||
|
|
||||||
figure_canvas_agg = FigureCanvasAgg(fig)
|
|
||||||
figure_canvas_agg.draw()
|
|
||||||
|
|
||||||
# Unfortunately, there's no accessor for the pointer to the native renderer
|
|
||||||
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
|
|
||||||
|
|
||||||
# time.sleep(.1)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -27,8 +27,8 @@ def PyplotSimple():
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
# evenly sampled time at 200ms intervals
|
# evenly sampled time .2 intervals
|
||||||
t = np.arange(0., 5., 0.2)
|
t = np.arange(0., 5., 0.2) # go from 0 to 5 using .2 intervals
|
||||||
|
|
||||||
# red dashes, blue squares and green triangles
|
# red dashes, blue squares and green triangles
|
||||||
plt.plot(t, t, 'r--', t, t ** 2, 'bs', t, t ** 3, 'g^')
|
plt.plot(t, t, 'r--', t, t ** 2, 'bs', t, t ** 3, 'g^')
|
||||||
|
@ -823,7 +823,7 @@ def AxesGrid():
|
||||||
|
|
||||||
|
|
||||||
# The magic function that makes it possible.... glues together tkinter and pyplot using Canvas Widget
|
# The magic function that makes it possible.... glues together tkinter and pyplot using Canvas Widget
|
||||||
def draw_figure(canvas, figure, loc=(0, 0)):
|
def draw_figure(canvas, figure):
|
||||||
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
|
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
|
||||||
figure_canvas_agg.draw()
|
figure_canvas_agg.draw()
|
||||||
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
|
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
|
||||||
|
@ -851,7 +851,7 @@ sg.ChangeLookAndFeel('LightGreen')
|
||||||
figure_w, figure_h = 650, 650
|
figure_w, figure_h = 650, 650
|
||||||
# define the form layout
|
# define the form layout
|
||||||
listbox_values = list(fig_dict)
|
listbox_values = list(fig_dict)
|
||||||
col_listbox = [[sg.Listbox(values=listbox_values, change_submits=True, size=(28, len(listbox_values)), key='-LISTBOX-')],
|
col_listbox = [[sg.Listbox(values=listbox_values, enable_events=True, size=(28, len(listbox_values)), key='-LISTBOX-')],
|
||||||
[sg.T(' ' * 12), sg.Exit(size=(5, 2))]]
|
[sg.T(' ' * 12), sg.Exit(size=(5, 2))]]
|
||||||
|
|
||||||
layout = [[sg.Text('Matplotlib Plot Test', font=('current 18'))],
|
layout = [[sg.Text('Matplotlib Plot Test', font=('current 18'))],
|
||||||
|
|
|
@ -1,16 +1,12 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import sys
|
#!/usr/bin/env python
|
||||||
if sys.version_info[0] >= 3:
|
import PySimpleGUI as sg
|
||||||
import PySimpleGUI as sg
|
|
||||||
import tkinter as Tk
|
|
||||||
else:
|
|
||||||
import PySimpleGUI27 as sg
|
|
||||||
import Tkinter as Tk
|
|
||||||
import matplotlib
|
import matplotlib
|
||||||
matplotlib.use('TkAgg')
|
matplotlib.use('TkAgg')
|
||||||
from matplotlib.backends.backend_tkagg import FigureCanvasAgg
|
|
||||||
import matplotlib.backends.tkagg as tkagg
|
|
||||||
import inspect
|
import inspect
|
||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Demonstrates one way of embedding Matplotlib figures into a PySimpleGUI window.
|
Demonstrates one way of embedding Matplotlib figures into a PySimpleGUI window.
|
||||||
|
@ -827,28 +823,15 @@ def AxesGrid():
|
||||||
return plt.gcf()
|
return plt.gcf()
|
||||||
|
|
||||||
# The magic function that makes it possible.... glues together tkinter and pyplot using Canvas Widget
|
# The magic function that makes it possible.... glues together tkinter and pyplot using Canvas Widget
|
||||||
def draw_figure(canvas, figure, loc=(0, 0)):
|
def draw_figure(canvas, figure):
|
||||||
""" 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)
|
|
||||||
|
|
||||||
# Position: convert from top-left anchor to center anchor
|
def delete_figure_agg(figure_agg):
|
||||||
canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo)
|
figure_agg.get_tk_widget().forget()
|
||||||
|
plt.close('all')
|
||||||
# Unfortunately, there's no accessor for the pointer to the native renderer
|
|
||||||
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
|
|
||||||
|
|
||||||
# Return a handle which contains a reference to the photo object
|
|
||||||
# which must be kept live or else the picture disappears
|
|
||||||
return photo
|
|
||||||
|
|
||||||
|
|
||||||
# -------------------------------- GUI Starts Here -------------------------------#
|
# -------------------------------- GUI Starts Here -------------------------------#
|
||||||
|
@ -871,40 +854,36 @@ fig_dict = {'Pyplot Simple':PyplotSimple, 'Pyplot Formatstr':PyplotFormatstr,'Py
|
||||||
sg.ChangeLookAndFeel('LightGreen')
|
sg.ChangeLookAndFeel('LightGreen')
|
||||||
figure_w, figure_h = 650, 650
|
figure_w, figure_h = 650, 650
|
||||||
# define the form layout
|
# define the form layout
|
||||||
listbox_values = [key for key in fig_dict.keys()]
|
listbox_values = list(fig_dict)
|
||||||
col_listbox = [[sg.Listbox(values=listbox_values, change_submits=True, size=(28, len(listbox_values)), key='func')],
|
col_listbox = [[sg.Listbox(values=listbox_values, change_submits=True, size=(28, len(listbox_values)), key='-LISTBOX-')],
|
||||||
[sg.T(' ' * 12), sg.Exit(size=(5, 2))]]
|
[sg.T(' ' * 12), sg.Exit(size=(5, 2))]]
|
||||||
|
|
||||||
col_multiline = sg.Column([[sg.Multiline(size=(70, 35), key='multiline')]])
|
col_multiline = sg.Column([[sg.Multiline(size=(70, 35), key='-MULTILINE-')]])
|
||||||
col_canvas = sg.Column([[ sg.Canvas(size=(figure_w, figure_h), key='canvas')]])
|
col_canvas = sg.Column([[ sg.Canvas(size=(figure_w, figure_h), key='-CANVAS-')]])
|
||||||
|
col_instructions = sg.Column([[sg.Pane([col_canvas, col_multiline], size=(800,600))],
|
||||||
|
[sg.Text('Grab square above and slide upwards to view source code for graph')]])
|
||||||
|
|
||||||
layout = [[sg.Text('Matplotlib Plot Test', font=('current 18'))],
|
layout = [[sg.Text('Matplotlib Plot Test', font=('ANY 18'))],
|
||||||
[sg.Column(col_listbox), sg.Pane([col_canvas, col_multiline], size=(800,600))],
|
[sg.Column(col_listbox), col_instructions],]
|
||||||
]
|
|
||||||
|
|
||||||
# create the form and show it without the plot
|
# create the form and show it without the plot
|
||||||
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI',resizable=True, grab_anywhere=False).Layout(layout)
|
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI',layout, resizable=True, finalize=True)
|
||||||
window.Finalize()
|
|
||||||
|
|
||||||
canvas_elem = window.FindElement('canvas')
|
canvas_elem = window.FindElement('-CANVAS-')
|
||||||
multiline_elem= window.FindElement('multiline')
|
multiline_elem= window.FindElement('-MULTILINE-')
|
||||||
|
figure_agg = None
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
event, values = window.Read()
|
event, values = window.Read()
|
||||||
# print(event)
|
|
||||||
# show it all again and get buttons
|
|
||||||
if event in (None, 'Exit'):
|
if event in (None, 'Exit'):
|
||||||
break
|
break
|
||||||
|
|
||||||
try:
|
if figure_agg:
|
||||||
choice = values['func'][0]
|
# ** IMPORTANT ** Clean up previous drawing before drawing again
|
||||||
func = fig_dict[choice]
|
delete_figure_agg(figure_agg)
|
||||||
except:
|
choice = values['-LISTBOX-'][0] # get first listbox item chosen (returned as a list)
|
||||||
pass
|
func = fig_dict[choice] # get function to call from the dictionary
|
||||||
|
window['-MULTILINE-'].Update(inspect.getsource(func)) # show source code to function in multiline
|
||||||
multiline_elem.Update(inspect.getsource(func))
|
fig = func() # call function to get the figure
|
||||||
plt.clf()
|
figure_agg = draw_figure(window['-CANVAS-'].TKCanvas, fig) # draw the figure
|
||||||
fig = func()
|
|
||||||
fig_photo = draw_figure(canvas_elem.TKCanvas, fig)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue