【问题标题】:two matplotlib figures in two different tk canvas两个不同的 tk 画布中的两个 matplotlib 图形
【发布时间】:2016-04-13 02:23:00
【问题描述】:

我试图调整 Hans 在 Matplotlib plot in Tkinter - every update adds new NavigationToolbar? 中的回答,以便在 2 个不同的画布上绘制 2 个不同的图。

我有一个问题:我不知道如何引用这两个不同的数字。

虽然 fig1 在 canvas1 中,而 fig2 在 canvas2 中,但 plt.plot 函数并没有引用它们 - plt.figure() 没有帮助。如何在画布中绘制一件事,而在另一件中绘制另一件事?

from Tkinter import Tk, Button
import numpy as np
import random
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg

def plotthem():
    plt.figure(1)
    plt.clf()
    x = np.arange(0.0,3.0,0.01)
    y = np.sin(2*np.pi*x+random.random())
    plt.plot(x,y)
    plt.gcf().canvas.draw()

    plt.figure(2)
    plt.clf()
    x = np.arange(0.0,3.0,0.01)
    y = np.tan(2*np.pi*x+random.random())
    plt.plot(x,y)
    plt.gcf().canvas.draw()

root = Tk()

b = Button(root, text="Plot", command = plotthem)
b.grid(row=0, column=0)

# init figures
fig1 = plt.figure()

canvas1 = FigureCanvasTkAgg(fig1, master=root)
toolbar = NavigationToolbar2TkAgg(canvas1, root)
canvas1.get_tk_widget().grid(row=0,column=1)
toolbar.grid(row=1,column=1)

fig2 = plt.figure()
canvas2 = FigureCanvasTkAgg(fig2, master=root)
toolbar = NavigationToolbar2TkAgg(canvas2, root)
canvas2.get_tk_widget().grid(row=0,column=2)
toolbar.grid(row=1,column=2)


root.mainloop()

【问题讨论】:

    标签: python canvas matplotlib tkinter


    【解决方案1】:

    好吧,我想通了... 您只需每次使用plt.gcf().canvas.draw(),因为gcf() 代表获取当前图形,并且与plt.figure( 协同工作,它能够更新不同画布中的不同图形。 p>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 2016-01-23
      • 2012-06-07
      • 2022-09-27
      • 2013-07-06
      相关资源
      最近更新 更多