如何在 tkinter (python) 中实现 pcolor 贴图旁边的颜色条?
How to implement colorbar next to pcolor map in tkinter (python)?
我正在使用 tkinter 制作一个交互式工具,并希望借助 pcolor 将屏蔽数据集映射到经度和纬度(基于列表框中的选择的屏蔽)。我想要地图旁边的颜色条。这个想法是,如果您单击 'select' 按钮,地图就会显示,如果您再次单击该按钮,地图就会更新。
我尝试使用 matshow 而不是 pcolor 来查看绘图是否有效。它起作用了,但是当我使用 crs=ccrs.PlateCarree() 时我没有起作用。对于更新部分,我尝试制作一个新的 canvas.
import tkinter as tk
from tkinter import Tk, Listbox, MULTIPLE, END, Button
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
import matplotlib.pyplot as plt
class ClassName:
def __init__(self,master):
self.master = master
canvas = tk.Canvas(master, width = 1000, height = 350) # create the canvas (tkinter module)
canvas.pack()
list = tk.Listbox(master,height=10,width=22)
list.pack(side=tk.LEFT)
for i in [2,3,4,5]:
list.insert(tk.END,i)
canvas.create_window(105,105,window=list)
select_button = tk.Button(master,text='Select variables',command=lambda: self.selection(list,master))
canvas.create_window(100,205,window= select_button)
def selection(self,list,master):
values= [list.get(idx) for idx in list.curselection()]
shape = np.random.randint(1, values[0], [5,5])
lon = np.arange(5)
lat = np.arange(5)
fig = Figure(figsize=(4,4))
a = fig.add_subplot(111)
tiler = cimgt.StamenTerrain(desired_tile_form='')
ax = plt.axes(projection=tiler.crs)
ax.set_extent((0,4,0,4),crs=ccrs.PlateCarree())
ax.add_image(tiler,10, cmap='gray')
c = ax.pcolor(lon,lat,shape,transform=ccrs.PlateCarree())
fig.colorbar(c)
canvas1 = tk.Canvas(master,width = 500, height = 400)
canvas1.pack(side=tk.LEFT)
canvas1 = FigureCanvasTkAgg(fig)
canvas1.get_tk_widget().pack(side=tk.LEFT)
canvas1.draw_idle()
canvas1.create_window(100,500,window=c)
root = Tk()
my_gui = ClassName(root)
root.mainloop()
我希望 pcolor 贴图显示时旁边有颜色条,并且这张贴图会更新。颜色条可以保留,但我不介意更新它。
我的实际结果是我得到了一堆错误并且没有绘制任何东西。
我做了不同的事情,现在颜色条的绘图工作了。这就是我所做的。
fig = Figure(figsize=(4,4))
ax = fig.add_subplot(111)
c = ax.pcolor(lon,lat,shape)
fig.colorbar(c,ax=ax,fraction=0.046,pad=0.04)
canvas = FigureCanvasTkAgg(fig,master)
canvas.get_tk_widget().grid(row=0,column=self.clicks)
我正在使用 tkinter 制作一个交互式工具,并希望借助 pcolor 将屏蔽数据集映射到经度和纬度(基于列表框中的选择的屏蔽)。我想要地图旁边的颜色条。这个想法是,如果您单击 'select' 按钮,地图就会显示,如果您再次单击该按钮,地图就会更新。
我尝试使用 matshow 而不是 pcolor 来查看绘图是否有效。它起作用了,但是当我使用 crs=ccrs.PlateCarree() 时我没有起作用。对于更新部分,我尝试制作一个新的 canvas.
import tkinter as tk
from tkinter import Tk, Listbox, MULTIPLE, END, Button
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
import matplotlib.pyplot as plt
class ClassName:
def __init__(self,master):
self.master = master
canvas = tk.Canvas(master, width = 1000, height = 350) # create the canvas (tkinter module)
canvas.pack()
list = tk.Listbox(master,height=10,width=22)
list.pack(side=tk.LEFT)
for i in [2,3,4,5]:
list.insert(tk.END,i)
canvas.create_window(105,105,window=list)
select_button = tk.Button(master,text='Select variables',command=lambda: self.selection(list,master))
canvas.create_window(100,205,window= select_button)
def selection(self,list,master):
values= [list.get(idx) for idx in list.curselection()]
shape = np.random.randint(1, values[0], [5,5])
lon = np.arange(5)
lat = np.arange(5)
fig = Figure(figsize=(4,4))
a = fig.add_subplot(111)
tiler = cimgt.StamenTerrain(desired_tile_form='')
ax = plt.axes(projection=tiler.crs)
ax.set_extent((0,4,0,4),crs=ccrs.PlateCarree())
ax.add_image(tiler,10, cmap='gray')
c = ax.pcolor(lon,lat,shape,transform=ccrs.PlateCarree())
fig.colorbar(c)
canvas1 = tk.Canvas(master,width = 500, height = 400)
canvas1.pack(side=tk.LEFT)
canvas1 = FigureCanvasTkAgg(fig)
canvas1.get_tk_widget().pack(side=tk.LEFT)
canvas1.draw_idle()
canvas1.create_window(100,500,window=c)
root = Tk()
my_gui = ClassName(root)
root.mainloop()
我希望 pcolor 贴图显示时旁边有颜色条,并且这张贴图会更新。颜色条可以保留,但我不介意更新它。
我的实际结果是我得到了一堆错误并且没有绘制任何东西。
我做了不同的事情,现在颜色条的绘图工作了。这就是我所做的。
fig = Figure(figsize=(4,4))
ax = fig.add_subplot(111)
c = ax.pcolor(lon,lat,shape)
fig.colorbar(c,ax=ax,fraction=0.046,pad=0.04)
canvas = FigureCanvasTkAgg(fig,master)
canvas.get_tk_widget().grid(row=0,column=self.clicks)