Repository navigation
Description
Bug report
Bug summary
I am trying to embed matplotlib in a Tk Canvas using FigureCanvasTkAgg. The Tk canvas itself is embedded in a Tk Frame, that can be resized. At certain dimensions of the Tk Frame the FigureCanvasTkAgg fails during the call to its draw function and throws the following exception:
File "...\python3.4\win64d\lib\site-packages\matplotlib-1.5.3.egg\matplotlib\backends\tkagg.py", line 30, in blit
id(data), colormode, id(bbox_array))
tkinter.TclError: buffer is of wrong type
I am using this functionality in a greater cae standalone program that uses Tk for its gui and the problem only happens when the cae files that we load on our program occupy around 2gb of RAM. When smaller files are loaded this issue does not appear. However if before loading the small files, we have loaded large files the issue keeps coming up even for the small ones.
I used the code from matplotlib site (https://matplotlib.org/1.5.3/examples/user_interfaces/embedding_in_tk.html) to demonstrate that the issue does not have to do with my code, since when the large models are loaded, this example also produces the same errors.
Now, after this error appears, if the user resizes the window and is lucky to find a correct dimension for the Tk window that contains the canvas, then the issue stops appearing and the FigureCanvasTkAgg draws itself successfully.
I understand that the exception is raised from this code https://github.com/matplotlib/matplotlib/blob/v1.5.3/lib/matplotlib/backends/tkagg.py (line 32)
and by searching a little bit I have found that the call to PyAggImagePhoto fails, probably in this code https://github.com/matplotlib/matplotlib/blob/v1.5.3/src/_tkagg.cpp (line 85).
If I am not mistaken, it tries to instantiate a numpy::array_view class passing it the figureCanvasTkAgg.renderer._renderer python object as a parameter, and somehow this throws the exception.
I have even tested with this code https://matplotlib.org/1.5.3/examples/user_interfaces/embedding_in_tk_canvas.html (it is using FigureCanvasAgg) and the issue still appears.
Code for reproduction
You can use the example from matplotlib site (https://matplotlib.org/1.5.3/examples/user_interfaces/embedding_in_tk.html)
# My code is a little bit different from the sites's
# since I am using a personal popupdialog that I have created to make use in
#our program, but the concept is the same
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
f = mpl.figure.Figure(figsize=(4, 3), dpi=100)
a = f.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
a.plot(t, s)
graphGui = PopUpDialog()
canvas = FigureCanvasTkAgg(f, master=graphGui.body)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)The canvas is not drawn and the following error appears
File "...\python3.4\win64d\lib\site-packages\matplotlib-1.5.3.egg\matplotlib\backends\tkagg.py", line 30, in blit
id(data), colormode, id(bbox_array))
tkinter.TclError: buffer is of wrong type
Expected outcome
The canvas should be drawn and the error should not appear
Matplotlib version
- Operating system: Windows 10 Pro 64-bit, 32 GB RAM,
- Graphics: NVIDIA QUADRO M2200, Resolution 2560 x 1440
- Matplotlib version: 1.5.3
- Matplotlib backend : TkAgg
- Python version: 3.4.3 (default, Feb 26 2015, 07:46:55) [MSC v.1600 64 bit (AMD64)]
- Jupyter version (if applicable):
- Other libraries: numpy 1.11.2, but also tested with numpy 1.13.3 and the same problem appears
Matplotlib has been installed through pip.
Activity
dopplershift commentedon Nov 16, 2017
Can you update your matplotlib to 2.1? Those are working for me on osx--though the
embedding_in_tk_sgskip.pyhas some pretty nasty flickering.eythimis commentedon Nov 17, 2017
Unfortunately, still the same issue
eythimis commentedon Nov 17, 2017
I have found something that might help,
When the canvas is not drawn on my Tk canvas, I tried to output it in a png image using the following command
FigureCanvasAgg.print_png(myCanvasObj,"c:/temp/test.png")and the image was correctly written to the png file.
Hence the data are there but cannot be plot in Tk canvas??
tacaswell commentedon Nov 20, 2017
Is that the full traceback you got?
Can you get any debugging hooks in to sort out exactly what is in
bufferobjwhen this goes wrong? It looks like this is an exception we producematplotlib/src/_tkagg.cpp
Lines 81 to 88 in 26382a7
When you are having these issues, how much free memory is there on the system?
Where does
PopUpDialog()come from?eythimis commentedon Nov 20, 2017
Yes unfortunately that is the full traceback I have got.
I believe (as seen from the code) that the bufferobj is argv[2] and from https://github.com/matplotlib/matplotlib/blob/v1.5.3/lib/matplotlib/backends/tkagg.py I can assume that the argument that is passed in position 2 is the memory position-
id(data)- from thedata = np.asarray(aggimage)as seen in https://github.com/matplotlib/matplotlib/blob/v1.5.3/lib/matplotlib/backends/tkagg.py.This aggimage is the _RendererAgg as taken from FigureCanvasTkAgg.renderer._renderer and converted to numpyarray. using
np.asarray(...).From a windbg session I have found that there is an ACCESS VIOLATION crash in numpy's multiarray
StackTrace
I can assume that probably
import_array(https://github.com/matplotlib/matplotlib/blob/v1.5.3/src/_tkagg.cpp, line 456) might causing some load failure, as I see it in a lot of github issues (for example, https://stackoverflow.com/questions/12879226/error-occurs-when-i-write-my-own-c-extension-for-numpy)Another thing though is that, when I get this exception and call
FigureCanvasTkAgg.draw()again and again, it finally succeeds...I have 32Gb of RAM and at the time that this error appears I have plenty of RAM available. The problem is if there is some Tkinter limitation on memory per application - which I don't know - that causes these issues, but surely a better explanation on the exception would clear things out. Do you believe it would be easy to send me a compiled version of Matplotlib which would explain the exception better?
PopUpDialog is an internal class of our program which is basically a Tk popupwindow with a 2 Tk Frames. The top Tk Frame is used to place FiguraCanvasTkAgg inside.
anntzer commentedon Dec 15, 2017
Can you check whether #9356 fixes this issue?
eythimis commentedon Oct 8, 2018
Matplotlib 3.0.0 resolves this issue