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
5 changed files with 102 additions and 148 deletions
|
@ -1,66 +1,50 @@
|
|||
#!/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
|
||||
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, FigureCanvasAgg
|
||||
from matplotlib.figure import Figure
|
||||
import matplotlib.backends.tkagg as tkagg
|
||||
import tkinter as tk
|
||||
|
||||
import PySimpleGUI as sg
|
||||
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
||||
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():
|
||||
# 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.ReadButton('Exit', size=(10, 2), pad=((280, 0), 3), font='Helvetica 14')]]
|
||||
[sg.Canvas(size=(640, 480), key='-CANVAS-')],
|
||||
[sg.Button('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()
|
||||
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
|
||||
# draw the intitial scatter plot
|
||||
fig, ax = plt.subplots()
|
||||
ax.grid(True)
|
||||
fig_agg = draw_figure(canvas, fig)
|
||||
|
||||
while True:
|
||||
event, values = window.Read(timeout=10)
|
||||
if event in ('Exit', None):
|
||||
exit(69)
|
||||
|
||||
def PyplotScatterWithLegend():
|
||||
import matplotlib.pyplot as plt
|
||||
from numpy.random import rand
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
for color in ['red', 'green', 'blue']:
|
||||
n = 750
|
||||
x, y = rand(2, n)
|
||||
scale = 200.0 * rand(n)
|
||||
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)
|
||||
ax.cla()
|
||||
ax.grid(True)
|
||||
for color in ['red', 'green', 'blue']:
|
||||
n = 750
|
||||
x, y = rand(2, n)
|
||||
scale = 200.0 * rand(n)
|
||||
ax.scatter(x, y, c=color, s=scale, label=color, alpha=0.3, edgecolors='none')
|
||||
ax.legend()
|
||||
fig_agg.draw()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue