Skip to content Skip to sidebar Skip to footer

Rotate Matplotlib Navigationtoolbar2tk To Make It Vertical

I have 3 plots like this in a Tkinter window Is it possible to orient the NavigationToolbar2Tk vertically so I can place it on the left of the plot ? I didn't find any documentati

Solution 1:

You need to create a derived class from NavigationToolbar2Tk to override the default horizontal orientation.

Below is an example (based on the "Embedding in Tk" example):

import tkinter as tk

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import numpy as np

classVerticalNavigationToolbar2Tk(NavigationToolbar2Tk):
   def__init__(self, canvas, window):
      super().__init__(canvas, window, pack_toolbar=False)

   # override _Button() to re-pack the toolbar button in vertical directiondef_Button(self, text, image_file, toggle, command):
      b = super()._Button(text, image_file, toggle, command)
      b.pack(side=tk.TOP) # re-pack button in vertical directionreturn b

   # override _Spacer() to create vertical separatordef_Spacer(self):
      s = tk.Frame(self, width=26, relief=tk.RIDGE, bg="DarkGray", padx=2)
      s.pack(side=tk.TOP, pady=5) # pack in vertical directionreturn s

   # disable showing mouse position in toolbardefset_message(self, s):
      pass

root = tk.Tk()
root.wm_title("Embedding in Tk")

fig = Figure(figsize=(5, 4), dpi=100)
t = np.arange(0, 3, .01)
fig.add_subplot().plot(t, 2 * np.sin(2 * np.pi * t))

canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.RIGHT, fill=tk.BOTH, expand=1)

toolbar = VerticalNavigationToolbar2Tk(canvas, root)
toolbar.update()
toolbar.pack(side=tk.LEFT, fill=tk.Y)

root.mainloop()

And the output:

enter image description here

Post a Comment for "Rotate Matplotlib Navigationtoolbar2tk To Make It Vertical"