Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] Plotting Raster Data / TIFF files on tkinter canvas
Hi All....I have a TIFF file which I am trying to plot on the tkinter canvas using the following code:

import tkinter

from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

root = tkinter.Tk()

fig = Figure(figsize=(5, 4), dpi=100)

canvas1 = FigureCanvasTkAgg(fig, master=root)

toolbar = NavigationToolbar2Tk(canvas1,root)
toolbar.pack(side=tkinter.TOP, fill=tkinter.X, padx=8)

canvas1.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1, padx=10, pady=5)

canvas1._tkcanvas.pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1, padx=10, pady=5)

def on_key_press(event):
    print("you pressed {}".format(event.key))
    key_press_handler(event, canvas1, toolbar)

canvas1.mpl_connect("key_press_event", on_key_press)

def _load():
    import rasterio as rio
    from rasterio.plot import show
    ax = fig.add_subplot(111)
    fig.subplots_adjust(bottom=0, right=1, top=1, left=0, wspace=0, hspace=0)

#    fig.tight_layout(pad=0)

    with'C:\Users\Desktop\t1.tif') as src_plot:
        show(src_plot, ax=ax, cmap='gist_gray')
    ax.set(title="",xticks=[], yticks=[])

button = tkinter.Button(master=root, text="Load", command=_load)

Issue which I am facing is that the raster/tiff file is getting plotted on the tkinter canvas, but it is placed inside the matplotlib figure window/frame whose size is not equal to tkinter canvas size. And hence if I am panning the figure inside the canvas, it is only moving inside the window and not across the canvas (as it used to be in ARCMAP/QGIS). Can someone help me out in resolving the issue.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  tkinter canvas help pythonprogrammer 5 217 Dec-23-2019, 08:32 AM
Last Post: Malt
  Unable fetch fucntion data in class in tkinter jenkins43 2 280 Nov-30-2019, 09:47 PM
Last Post: jenkins43
  tkinter GUI, problem running seperate files fishglue 17 746 Oct-15-2019, 02:56 PM
Last Post: Denni
  [Tkinter] Scrollbar doesn't work on Canvas in Tkinter DeanAseraf1 3 2,445 Sep-19-2019, 03:26 PM
Last Post: joe_momma
  TKinter GUI / Pandas Data frame Loop NSearch 0 1,569 Jun-26-2019, 12:37 AM
Last Post: NSearch
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 6,668 Jun-04-2019, 05:05 AM
Last Post: Gupi
  Axis lim and Plotting a graph in Tkinter KEDivergente 0 336 May-21-2019, 08:10 PM
Last Post: KEDivergente
  Append Data from TkInter to Excel zinho 5 9,584 Feb-16-2017, 01:47 PM
Last Post: johnathon

Forum Jump:

Users browsing this thread: 1 Guest(s)