NEW Demo Program for PyPlot diagrapms. Displays bar chart
This commit is contained in:
parent
7c783dcaf2
commit
087a0e218a
|
@ -0,0 +1,88 @@
|
|||
#!/usr/bin/env python
|
||||
import sys
|
||||
|
||||
if sys.version_info[0] >= 3:
|
||||
import PySimpleGUI as sg
|
||||
else:
|
||||
import PySimpleGUI27 as sg
|
||||
|
||||
import matplotlib
|
||||
|
||||
matplotlib.use('TkAgg')
|
||||
from matplotlib.backends.backend_tkagg import FigureCanvasAgg
|
||||
import matplotlib.backends.tkagg as tkagg
|
||||
import tkinter as Tk
|
||||
|
||||
"""
|
||||
Demonstrates one way of embedding Matplotlib figures into a PySimpleGUI window.
|
||||
|
||||
Paste your Pyplot code into the section marked below.
|
||||
|
||||
Do all of your plotting as you normally would, but do NOT call plt.show().
|
||||
Stop just short of calling plt.show() and let the GUI do the rest.
|
||||
|
||||
The remainder of the program will convert your plot and display it in the GUI.
|
||||
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
|
||||
|
||||
N = 5
|
||||
values_to_plot = (20, 35, 30, 35, 27)
|
||||
ind = np.arange(N) # the x locations for the groups
|
||||
width = 0.4 # the width of the bars: can also be len(x) sequence
|
||||
|
||||
p1 = plt.bar(ind, values_to_plot, width)
|
||||
|
||||
plt.ylabel('Y-Axis Values')
|
||||
plt.title('Plot Title')
|
||||
plt.xticks(ind, ('Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'))
|
||||
plt.yticks(np.arange(0, 81, 10))
|
||||
plt.legend((p1[0],), ('Data Group 1',))
|
||||
|
||||
|
||||
# ------------------------------- END OF YOUR MATPLOTLIB CODE -------------------------------
|
||||
|
||||
# ------------------------------- 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.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
|
||||
|
||||
|
||||
# ------------------------------- Beginning of GUI CODE -------------------------------
|
||||
|
||||
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.OK(pad=((figure_w / 2, 0), 3), size=(4, 2))]]
|
||||
|
||||
# create the form and show it without the plot
|
||||
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI', force_toplevel=True).Layout(
|
||||
layout).Finalize()
|
||||
|
||||
# add the plot to the window
|
||||
fig_photo = draw_figure(window.FindElement('canvas').TKCanvas, fig)
|
||||
|
||||
# show it all again and get buttons
|
||||
button, values = window.Read()
|
Loading…
Reference in New Issue