I am trying to embed a meshed plot into the tkinter convas. I have the plot in this code:
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()
I have tried the below code to show that on the convas but I get an error. I think there is a problem to connect the method I am plotting to the Figure
. Anybody knows how to fix that? thank you
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()
CodePudding user response:
You need to use ax
instead of plt
in order to embed it into 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()
...
Result: