旋转 matplotlib NavigationToolbar2Tk 使其垂直 [英] Rotate matplotlib NavigationToolbar2Tk to make it vertical

查看:47
本文介绍了旋转 matplotlib NavigationToolbar2Tk 使其垂直的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Tkinter 窗口中有 3 个这样的图

I have 3 plots like this in a Tkinter window

是否可以垂直定位 NavigationToolbar2Tk,以便我可以将其放置在绘图的左侧?

Is it possible to orient the NavigationToolbar2Tk vertically so I can place it on the left of the plot ?

我没有找到任何关于类似内容的文档或主题.

I didn't find any documentation or thread about something similar.

谢谢

推荐答案

您需要从 NavigationToolbar2Tk 创建一个派生类来覆盖默认的水平方向.

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

下面是一个示例(基于嵌入 Tk" 示例):

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

class VerticalNavigationToolbar2Tk(NavigationToolbar2Tk):
   def __init__(self, canvas, window):
      super().__init__(canvas, window, pack_toolbar=False)

   # override _Button() to re-pack the toolbar button in vertical direction
   def _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 direction
      return b

   # override _Spacer() to create vertical separator
   def _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 direction
      return s

   # disable showing mouse position in toolbar
   def set_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()

和输出:

这篇关于旋转 matplotlib NavigationToolbar2Tk 使其垂直的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆