Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FigureCanvasTkAgg - "buffer is of wrong type" error during blit #9799

Closed
@eythimis

Description

@eythimis

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions