Appart from the warning given as indicated in here, now the module matplotlib.backends.tkagg is not working with the example Demo_Matplotlib_Animated.py with matplotlib 3.1.0, but it works with version 3.0.3. The error given is
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\site-packages\matplotlib\backends\tkagg.py", line 27, in blit
dataptr, colormode, bboxptr)
_tkinter.TclError: invalid command name "PyAggImagePhoto"
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\gui.py", line 59, in <module>
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
File "C:\Program Files\Python37\lib\site-packages\matplotlib\backends\tkagg.py", line 28, in blit
except tk.TclError:
AttributeError: '_tkinter.tkapp' object has no attribute 'TclError'
I'll take a look and see what new technique is being suggested for Matplotlib integration.
Demo programs are meant to just get people started without having to learn these integration bits. Still, I do want it to function on later releases of matplotlib.
If you run across what considered to be the "new" way of integrating with 3.1.0, that would be great to see as it may make it possible for me to integrate with it again.
I needed to finish a sketch GUI so I just rolled back to matplotlib 3.0.3, if I have some time off I'll try to look at that. Thanks for such a great module!
Actually I raised a similar or probably identical issue (issue # 1713) today and since then kept searching internet many hours until I see this now. Immediate I rolled Matplotlib from 3.1.1 back to 3.0 3 and run a demo example. It works. My sg.Canvas can display a Matplotlib plot on it. I try many other internet examples and all work. So happy.
Ran into the same error, and hacked up a version that works based on this information https://matplotlib.org/3.1.0/gallery/user_interfaces/embedding_in_tk_sgskip.html
attached
demomatplotlib.zip
I have on my list to look at the Matplotlib code that was submitted not long back. I recall the browser being reworked at my request for help. I owe a look at that one and now I've got another. Thanks so much for submitting it!!
That wasn't a hack... that was precision surgery. I think it was 5 lines changed in the end. I removed the need to import tkinter by changing the 2 constants that were being used. I know the values. Thank you for this. I've tested with 3.1.1 and it works. It also worked with 3.0.3! The Demo_Matplotlib.py file has been modified and checked in. I'll update other demos as well. I was hoping a couple lines like this would be needed. I was just about to turn attention back to this as I know it's been sitting there needing completing.
#!/usr/bin/env python
import PySimpleGUI as sg
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
"""
Demonstrates one way of embedding Matplotlib figures into a PySimpleGUI window.
Basic steps are:
* Create a Canvas Element
* Layout form
* Display form (NON BLOCKING)
* Draw plots onto convas
* Display form (BLOCKING)
Based on information from: https://matplotlib.org/3.1.0/gallery/user_interfaces/embedding_in_tk_sgskip.html
(Thank you dirck)
"""
#------------------------------- PASTE YOUR MATPLOTLIB CODE HERE -------------------------------
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter # useful for `logit` scale
# Fixing random state for reproducibility
np.random.seed(19680801)
# make up some data in the interval ]0, 1[
y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))
# plot with various axes scales
plt.figure(1)
# linear
plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')
plt.grid(True)
# log
plt.subplot(222)
plt.plot(x, y)
plt.yscale('log')
plt.title('log')
plt.grid(True)
# symmetric log
plt.subplot(223)
plt.plot(x, y - y.mean())
plt.yscale('symlog', linthreshy=0.01)
plt.title('symlog')
plt.grid(True)
# logit
plt.subplot(224)
plt.plot(x, y)
plt.yscale('logit')
plt.title('logit')
plt.grid(True)
plt.gca().yaxis.set_minor_formatter(NullFormatter())
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,
wspace=0.35)
fig = plt.gcf() # if using Pyplot then get the figure from the plot
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
#------------------------------- END OF YOUR MATPLOTLIB CODE -------------------------------
#------------------------------- Beginning of Matplotlib helper code -----------------------
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)
#------------------------------- Beginning of GUI CODE -------------------------------
# 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', layout, force_toplevel=True, finalize=True)
# add the plot to the window
fig_photo = draw_figure(window['canvas'].TKCanvas, fig)
event, values = window.read()
Glad to be of service :-)
I have also checked in a new version of the Matplotlib Browser demo that utilizes the technique you've posted. It needed to have a new function added that will erase the previous plot. I didn't have to do that before because I didn't do a pack like this technique uses.
Animated versions updated... in fact ALL matplotlib demos were updated and improved
It is closed ticket already but I wanted to check the solution for it.
Because I encountered the problem on several windows PCs with matplotlib version 3.1.1 while trying the code on cookbook. (win10, python 3.7.4 ,VScode)
P.S.
OK, I found the page that says matplotlib 3.0.3 is necessary to make it work. I don't want to roll back so will wait the sample code that works with the default setting.
https://stackoverflow.com/questions/57395326/graphing-code-using-pysimplegui-matplotlib-and-tkinter-crashing-on-selecting-fu

PLEASE open a new Issue is you want something looked at. Closed issues are, well, closed.
One thing that opening a new issue does is it gets you to step through the troubleshooting process by giving you a checklist of places to look for a solution.
One such place you would have been sent to is the Demo Programs area. These programs demonstrate integrating PySimpleGUI with a number of other packages including Matplotlib. There are a number of demo programs posted that provide you the code needed to run with Matplotlib 3.1.1.
There is also an Issue (at least one) that speaks directly to the 3.1.1 problem and its eventual solution.
The Cookbook is the new priority to get up to date. It is clearly a bit behind the times and this weekend it's the target to get completed.
If you still have issues after checking the demos, then please open a new issue so that your specific problem can be examined.
I did some more investigation before making a new issue as suggested.
Actually I was trying the code from Cookbook when I had the problem.
However the code (https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Matplotlib_Animated.py) works file.
I wonder if the cookbook page could point to the code in repository so that the reader can get the latest version of code.
I'll make sure the cookbook's version gets updated. I worked right up to that specific Matplotlib graph yesterday and released it. Still working on the updates with more coming throughout the week ahead.
Most helpful comment
I'll make sure the cookbook's version gets updated. I worked right up to that specific Matplotlib graph yesterday and released it. Still working on the updates with more coming throughout the week ahead.