"template" version of Matplotlib Demo

Able to re-create all of the "API demos" from this page:
https://matplotlib.org/gallery/index.html
This commit is contained in:
MikeTheWatchGuy 2018-08-27 12:10:20 -04:00
parent 5243798a73
commit 82e326a6c1
1 changed files with 21 additions and 15 deletions

View File

@ -1,9 +1,7 @@
import PySimpleGUI as g import PySimpleGUI as g
import matplotlib import matplotlib
matplotlib.use('TkAgg') matplotlib.use('TkAgg')
from numpy import arange, sin, pi from matplotlib.backends.backend_tkagg import FigureCanvasAgg
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, FigureCanvasAgg
from matplotlib.figure import Figure
import matplotlib.backends.tkagg as tkagg import matplotlib.backends.tkagg as tkagg
import tkinter as Tk import tkinter as Tk
@ -42,22 +40,30 @@ def draw_figure(canvas, figure, loc=(0, 0)):
# which must be kept live or else the picture disappears # which must be kept live or else the picture disappears
return photo return photo
f = Figure(figsize=(5, 4), dpi=100) #------------------------------- PASTE YOUR MATPLOTLIB CODE HERE -------------------------------
a = f.add_subplot(111) import numpy as np
t = arange(0.0, 3.0, 0.01) import matplotlib
s = sin(2*pi*t) import matplotlib.pyplot as plt
a.plot(t, s) # Fixing random state for reproducibility
a.set_title('Tk embedding') np.random.seed(19680801)
a.set_xlabel('X axis label')
a.set_ylabel('Y label')
# -------------------------------- GUI Starts Here --------------------------------
canvas_elem = g.Canvas(size=(500, 400)) # get the canvas we'll be drawing on matplotlib.rcParams['axes.unicode_minus'] = False
fig, ax = plt.subplots()
ax.plot(10*np.random.randn(100), 10*np.random.randn(100), 'o')
ax.set_title('Using hyphen instead of Unicode minus')
# -------------------------------- GUI Starts Here -------------------------------#
# fig = your figure you want to display. Assumption is that 'fig' holds the #
# information to display. #
# --------------------------------------------------------------------------------#
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
canvas_elem = g.Canvas(size=(figure_w, figure_h)) # get the canvas we'll be drawing on
# define the form layout # define the form layout
layout = [[g.Text('Plot test')], layout = [[g.Text('Plot test')],
[canvas_elem], [canvas_elem],
[g.OK(pad=((250,0), 3))]] [g.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
form = g.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI') form = g.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI')
@ -65,7 +71,7 @@ form.Layout(layout)
form.ReadNonBlocking() form.ReadNonBlocking()
# add the plot to the window # add the plot to the window
fig_photo = draw_figure(canvas_elem.TKCanvas, f) fig_photo = draw_figure(canvas_elem.TKCanvas, fig)
# show it all again and get buttons # show it all again and get buttons
button, values = form.Read() button, values = form.Read()