【问题标题】:Display Matplotlib spines in Tkinter在 Tkinter 中显示 Matplotlib 刺
【发布时间】:2015-02-04 12:02:46
【问题描述】:

我知道如何在 Matplotlib 中显示刺。我也知道如何在 Tkinter 中显示 Matplotlib 子图。但我想知道如何在 Tkinter 的这个子图中放置刺。

这是在 Tkinter 中显示子图的代码:

import matplotlib
matplotlib.use('TkAgg')

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk

def destroy(e): sys.exit()

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

f = Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)
t = arange(0.0,3.0,0.01)
s = sin(2*pi*t)

a.plot(t,s)
a.set_title('Tk embedding')
a.set_xlabel('X axis label')
a.set_ylabel('Y label')

# a tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

#toolbar = NavigationToolbar2TkAgg( canvas, root )
#toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

button = Tk.Button(master=root, text='Quit', command=sys.exit)
button.pack(side=Tk.BOTTOM)

Tk.mainloop()`

这是在 Matplotlib 中显示脊椎的代码:

import numpy as np
import matplotlib.pyplot as plt


fig, ax = plt.subplots()

image = np.random.uniform(size=(10, 10))
ax.imshow(image, cmap=plt.cm.gray, interpolation='nearest')
ax.set_title('dropped spines')

# Move left and bottom spines outward by 10 points
ax.spines['left'].set_position(('outward', 10))
ax.spines['bottom'].set_position(('outward', 10))
# Hide the right and top spines
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
# Only show ticks on the left and bottom spines
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')

plt.show()

【问题讨论】:

    标签: python-3.x matplotlib tkinter


    【解决方案1】:

    在第二个代码块中使用ax.set_title('...') 时,在第一个代码块中使用a.set_title('...')。这几乎放弃了您可以在ax 上调用的方法,您也可以在a 上调用。

    只需使用与第二块相同的代码,但将ax 替换为a,它应该可以正常工作。


    根据文档,axa 不是完全相同的对象。 Figure.add_subplot() 返回一个 Axes 实例,pyplot.subplots() 返回一个 Axis 对象作为第二个输出参数。然而,由于

    Axes 包含大多数图形元素:Axis...

    您可以从两者中以相同的方式编辑脊椎。

    【讨论】:

      猜你喜欢
      • 2018-03-02
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      相关资源
      最近更新 更多