Tkinter AttributeError: 'AxesSubplot' object has no attribute 'canvas' and matplotlib graphs don't update until I resize the tkinter window
Tkinter AttributeError: 'AxesSubplot' object has no attribute 'canvas' and matplotlib graphs don't update until I resize the tkinter window
tkinter 上的 Matplotlib 图 canvas 给出错误 - 'AxesSubplot' 对象没有属性 'canvas'
# Create window
window = tkinter.Tk()
window.resizable()
window.state('zoomed')
# Create Title
window.title('Title')
# adding frame of buttons
BFrame = tkinter.Frame(window)
BFrame.pack(side=tkinter.TOP)
# This defines the Python GUI backend to use for matplotlib
matplotlib.use('TkAgg')
# Initialize matplotlib figure for graphing purposes
fig = Figure(figsize=(5,3), dpi = 100)
global sbpt_1, sbpt_2, sbpt_3
sbpt_1 = fig.add_subplot(1, 3, 1)
sbpt_2 = fig.add_subplot(1, 3, 2)
sbpt_3 = fig.add_subplot(1, 3, 3)
#Rotating x-ticks
sbpt_1.tick_params(axis='x', rotation=45)
sbpt_2.tick_params(axis='x', rotation=45)
sbpt_3.tick_params(axis='x', rotation=45)
#Subplot Titles
sbpt_1.title.set_text('Title A \n')
sbpt_2.title.set_text('Title B \n')
sbpt_3.title.set_text('Title C \n')
fig.suptitle('VISUALIZE FILES')
# Tight layout often produces nice results
# but requires the title to be spaced accordingly
fig.tight_layout()
fig.subplots_adjust(top=0.82)
# Special type of "canvas" to allow for matplotlib graphing
display = FigureCanvasTkAgg(fig, master=window)
display.draw()
display.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
#Navigator
toolbar = NavigationToolbar2Tk(display, window)
toolbar.update()
display._tkcanvas.pack(side=BOTTOM, fill=BOTH, expand=1)
.................
def plot_utm():
content.plot (color = '#3BB9FF', ax = sbpt_1)
sbpt_1.canvas.draw()
def plot_tm():
shp_TM.plot(color = '#4CC417', ax = sbpt_2)
sbpt_2.canvas.draw()
def plot_om():
shp_TMOM.plot(color = '#FFA62F', ax = sbpt_3)
sbpt_3.canvas.draw()
.........
# To make the window running
window.mainloop()
出现此错误。:
在 Tkinter 上绘图 Canvas 即使在单击可视化按钮后也是如此:
完整代码的这些行中存在问题。
如果我 运行 这段代码,它会给出错误 - 'AxesSubplot' object has no attribute 'canvas'
单击可视化按钮后,图不会出现在 canvas 上,但如果我使用 minimize/maximize 按钮调整 window 的大小,图就会出现。
问题已通过更新功能解决
def plot_utm():
sbpt_1 = fig.add_subplot(1, 3, 1)
content.plot (color = '#3BB9FF', ax = sbpt_1)
display.draw_idle()
def plot_tm():
sbpt_2 = fig.add_subplot(1, 3, 2)
shp_TM.plot(color = '#4CC417', ax = sbpt_2)
display.draw_idle()
def plot_om():
sbpt_3 = fig.add_subplot(1, 3, 3)
shp_TMOM.plot(color = '#FFA62F', ax = sbpt_3)
display.draw_idle()
tkinter 上的 Matplotlib 图 canvas 给出错误 - 'AxesSubplot' 对象没有属性 'canvas'
# Create window
window = tkinter.Tk()
window.resizable()
window.state('zoomed')
# Create Title
window.title('Title')
# adding frame of buttons
BFrame = tkinter.Frame(window)
BFrame.pack(side=tkinter.TOP)
# This defines the Python GUI backend to use for matplotlib
matplotlib.use('TkAgg')
# Initialize matplotlib figure for graphing purposes
fig = Figure(figsize=(5,3), dpi = 100)
global sbpt_1, sbpt_2, sbpt_3
sbpt_1 = fig.add_subplot(1, 3, 1)
sbpt_2 = fig.add_subplot(1, 3, 2)
sbpt_3 = fig.add_subplot(1, 3, 3)
#Rotating x-ticks
sbpt_1.tick_params(axis='x', rotation=45)
sbpt_2.tick_params(axis='x', rotation=45)
sbpt_3.tick_params(axis='x', rotation=45)
#Subplot Titles
sbpt_1.title.set_text('Title A \n')
sbpt_2.title.set_text('Title B \n')
sbpt_3.title.set_text('Title C \n')
fig.suptitle('VISUALIZE FILES')
# Tight layout often produces nice results
# but requires the title to be spaced accordingly
fig.tight_layout()
fig.subplots_adjust(top=0.82)
# Special type of "canvas" to allow for matplotlib graphing
display = FigureCanvasTkAgg(fig, master=window)
display.draw()
display.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
#Navigator
toolbar = NavigationToolbar2Tk(display, window)
toolbar.update()
display._tkcanvas.pack(side=BOTTOM, fill=BOTH, expand=1)
.................
def plot_utm():
content.plot (color = '#3BB9FF', ax = sbpt_1)
sbpt_1.canvas.draw()
def plot_tm():
shp_TM.plot(color = '#4CC417', ax = sbpt_2)
sbpt_2.canvas.draw()
def plot_om():
shp_TMOM.plot(color = '#FFA62F', ax = sbpt_3)
sbpt_3.canvas.draw()
.........
# To make the window running
window.mainloop()
出现此错误。:
在 Tkinter 上绘图 Canvas 即使在单击可视化按钮后也是如此:
完整代码的这些行中存在问题。 如果我 运行 这段代码,它会给出错误 - 'AxesSubplot' object has no attribute 'canvas' 单击可视化按钮后,图不会出现在 canvas 上,但如果我使用 minimize/maximize 按钮调整 window 的大小,图就会出现。
问题已通过更新功能解决
def plot_utm():
sbpt_1 = fig.add_subplot(1, 3, 1)
content.plot (color = '#3BB9FF', ax = sbpt_1)
display.draw_idle()
def plot_tm():
sbpt_2 = fig.add_subplot(1, 3, 2)
shp_TM.plot(color = '#4CC417', ax = sbpt_2)
display.draw_idle()
def plot_om():
sbpt_3 = fig.add_subplot(1, 3, 3)
shp_TMOM.plot(color = '#FFA62F', ax = sbpt_3)
display.draw_idle()