将网格图嵌入到 tkinter 画布中?

embeding a meshed plot into tkinter convas?

我正在尝试将网格图嵌入到 tkinter convas 中。我在这段代码中有情节:

from tkinter import *
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap

root = Tk()
root.geometry("400x400")

def plot():
    x, y = np.mgrid[slice(0, 4, 1), slice(0, 3, 1)]
    z = np.array([[1,2],[3,4],[5,6]])

    col_type = cm.get_cmap('rainbow', 256)
    newcolors = col_type(np.linspace(0, 1, 1000))
    white = np.array([1, 1, 1, 1])
    newcolors[:1, :] = white
    newcmp = ListedColormap(newcolors)

    c = plt.pcolormesh(x, y, z, cmap=newcmp, edgecolor='lightgrey', linewidth=0.003)
    plt.colorbar(c)

    plt.title('My Title', fontweight="bold")
    plt.xlabel("X", fontsize=14)
    plt.ylabel("Y", fontsize=14)
    plt.gca().invert_yaxis()
    plt.show()

button = Button(root, text="Plot", command=plot)
button.pack()

root.mainloop()

我已尝试使用以下代码在 convas 上显示该代码,但出现错误。我认为将我正在绘制的方法连接到 Figure 时出现问题。任何人都知道如何解决这个问题?谢谢

from tkinter import *
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap

root = Tk()
root.geometry("400x400")

def plot():
    x, y = np.mgrid[slice(0, 4, 1), slice(0, 3, 1)]
    z = np.array([[1,2],[3,4],[5,6]])

    figure = Figure(figsize=(12, 8))
    ax = figure.add_subplot(111)

    col_type = cm.get_cmap('rainbow', 256)
    newcolors = col_type(np.linspace(0, 1, 1000))
    white = np.array([1, 1, 1, 1])
    newcolors[:1, :] = white
    newcmp = ListedColormap(newcolors)

    c = plt.pcolormesh(x, y, z, cmap=newcmp, edgecolor='lightgrey', linewidth=0.003)
    ax.plt.colorbar(c)

    plt.title('mY Title', fontweight="bold")
    plt.xlabel("X", fontsize=14)
    plt.ylabel("Y", fontsize=14)
    plt.gca().invert_yaxis()
    canvas = FigureCanvasTkAgg(figure, root)
    canvas.get_tk_widget().pack()

button = Button(root, text="Plot", command=plot)
button.pack()

root.mainloop()

您需要使用 ax 而不是 plt 才能将其嵌入到 tkinter 中:

...
root = Tk()
# use minsize() instead of geometry() so that window can be expanded to fit the final plot size
root.minsize(400, 400) 

def plot():
    x, y = np.mgrid[slice(0, 4, 1), slice(0, 3, 1)]
    z = np.array([[1,2],[3,4],[5,6]])

    figure = Figure()
    ax = figure.add_subplot(111)

    col_type = cm.get_cmap('rainbow', 256)
    newcolors = col_type(np.linspace(0, 1, 1000))
    white = np.array([1, 1, 1, 1])
    newcolors[:1, :] = white
    newcmp = ListedColormap(newcolors)

    # use ax instead of plt in below lines
    c = ax.pcolormesh(x, y, z, cmap=newcmp, edgecolor='lightgrey', linewidth=0.003)
    ax.figure.colorbar(c)

    ax.set_title('My Title', fontweight="bold")
    ax.set_xlabel("X", fontsize=14)
    ax.set_ylabel("Y", fontsize=14)
    ax.invert_yaxis()

    canvas = FigureCanvasTkAgg(figure, root)
    canvas.get_tk_widget().pack()
...

结果: