I am making an interactive tool with tkinter, and want to map a masked data set against the longitude and latitude (masking based on selection in list boxes) with the help of pcolor. I want a colorbar next to the map. The idea is that if you click the 'select' button that the map will be shown and that if you click the button again that the map is updated.
I tried to use matshow instead of pcolor in order to see if the plotting worked or not. It worked, however I did not work when I used crs=ccrs.PlateCarree(). For the update part I tried to make a new 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()
I expect the pcolor map to be shown with the colorbar next to it and this map to update. The colorbar can stay however, I don't mind to update that.
My actual results are that I get a bunch of errors and nothing is plotted.
I did it differently and now the plotting of the colorbar works. This is what I did.
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)