python - 在 tkinter GUI 中导入 matplotlib 图。显示错误()

标签 python user-interface matplotlib tkinter show

我正在尝试在我的 tkinter GUI 中嵌入一个 matplotlib 图形。在对 matplotlib 网站上发布的示例进行多次尝试后,它们似乎都不起作用。

例如:

#!/usr/bin/env python

import matplotlib
matplotlib.use('TkAgg')

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
# implement the default mpl key bindings
from matplotlib.backend_bases import key_press_handler


from matplotlib.figure import Figure

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk

root = Tk.Tk()
root.wm_title("Embedding in TK")


f = Figure(figsize=(5, 4), dpi=100)
a = f.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)

a.plot(t, s)


# a tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)


def on_key_event(event):
    print('you pressed %s' % event.key)
    key_press_handler(event, canvas, toolbar)

canvas.mpl_connect('key_press_event', on_key_event)


def _quit():
    root.quit()     # stops mainloop
    root.destroy()  # this is necessary on Windows to prevent
                    # Fatal Python Error: PyEval_RestoreThread: NULL tstate

button = Tk.Button(master=root, text='Quit', command=_quit)
button.pack(side=Tk.BOTTOM)

Tk.mainloop()
# If you put root.destroy() here, it will cause an error if
# the window is closed with the window manager. 

上面的例子给我这个错误:

Traceback (most recent call last):
  File "<tmp 1>", line 34, in <module>
    canvas.show()
  File "C:\pyzo2015a\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 350, in draw
    tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
  File "C:\pyzo2015a\lib\site-packages\matplotlib\backends\tkagg.py", line 24, in blit
    tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
_tkinter.TclError

另一个例子:

#!/usr/bin/env python
# -*- noplot -*-

import matplotlib as mpl
import numpy as np
import sys
if sys.version_info[0] < 3:
    import Tkinter as tk
else:
    import tkinter as tk
import matplotlib.backends.tkagg as tkagg
from matplotlib.backends.backend_agg import FigureCanvasAgg


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)

    # Position: convert from top-left anchor to center anchor
    canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo)

    # Unfortunatly, there's no accessor for the pointer to the native renderer
    tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)

    # Return a handle which contains a reference to the photo object
    # which must be kept live or else the picture disappears
    return photo

# Create a canvas
w, h = 300, 200
window = tk.Tk()
window.title("A figure in a canvas")
canvas = tk.Canvas(window, width=w, height=h)
canvas.pack()

# Generate some example data
X = np.linspace(0, 2.0*3.14, 50)
Y = np.sin(X)

# Create the figure we desire to add to an existing canvas
fig = mpl.figure.Figure(figsize=(2, 1))
ax = fig.add_axes([0, 0, 1, 1])
ax.plot(X, Y)

# Keep this handle alive, or else figure will disappear
fig_x, fig_y = 100, 100
fig_photo = draw_figure(canvas, fig, loc=(fig_x, fig_y))
fig_w, fig_h = fig_photo.width(), fig_photo.height()

# Add more elements to the canvas, potentially on top of the figure
canvas.create_line(200, 50, fig_x + fig_w / 2, fig_y + fig_h / 2)
canvas.create_text(200, 50, text="Zero-crossing", anchor="s")

# Let Tk take over
tk.mainloop()

这个给了我这个错误:

 Traceback (most recent call last):
      File "<tmp 2>", line 56, in <module>
        fig_photo = draw_figure(canvas, fig, loc=(fig_x, fig_y))
      File "<tmp 2>", line 32, in draw_figure
        tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
      File "C:\pyzo2015a\lib\site-packages\matplotlib\backends\tkagg.py", line 24, in blit
        tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
    _tkinter.TclError

我不明白为什么这些示例不起作用。有人可以帮帮我吗?

最佳答案

我有类似的问题,所以这对我有用, 如果您使用的是 Windows,我建议卸载 pillowmatplotlib 并从 here 重新安装

关于python - 在 tkinter GUI 中导入 matplotlib 图。显示错误(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37497214/

相关文章:

python - 在 Python 中是否有用于格式化的键盘快捷键?

c# - 如何从另一个线程更新 GUI?

python-3.x - Python pyplot : Handling double-click event catches also the first click event

python - Nuke自己的python版本,如何安装、添加或包装新模块?

python - numpy 矩阵乘法简化 - 这可能吗?

python - tensorflow 对象检测 API train.py 仅使用 CPU 完成训练需要多长时间?

c# - 一个可执行文件可以既是控制台又是 GUI 应用程序吗?

java - 在 .NET Form/Visual Studio 编辑器中查看 Java GUI?

python - 使用 matplotlib 修剪尾随 xticks 零

python - 如何在 matplotlib.pyplot 中设置 X 和 Y 轴标题