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.
# 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 samefromnumpyimportarange, sin, pifrommatplotlib.backends.backend_tkaggimportFigureCanvasTkAggimportsysifsys.version_info[0] <3:
importTkinterasTkelse:
importtkinterasTkf=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
From a windbg session I have found that there is an ACCESS VIOLATION crash in numpy's multiarray
(numpy-1.11.2-py3.4-win-amd64.egg\numpy\core\multiarray.pyd )
Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
multiarray+0x457a0:
00007ffca27057a0 488b4908 mov rcx,qword ptr [rcx+8] ds:0000000000000007=????????????????
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.
Activity
dopplershift commented on Nov 16, 2017
Can you update your matplotlib to 2.1? Those are working for me on osx--though the embedding_in_tk_sgskip.py has some pretty nasty flickering.
eythimis commented on Nov 17, 2017
Unfortunately, still the same issue
eythimis commented on 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 commented on Nov 20, 2017
Is that the full traceback you got?
Can you get any debugging hooks in to sort out exactly what is in bufferobj when this goes wrong? It looks like this is an exception we produce
matplotlib/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 commented on 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 the data = 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 commented on Dec 15, 2017
Can you check whether #9356 fixes this issue?
eythimis commented on Oct 8, 2018
Matplotlib 3.0.0 resolves this issue