gpt4 book ai didi

python - 当在 tkinter 中嵌入的两个 matplotlib 图形之间切换时,第一个图形上会出现一组额外的意外 x 轴标签和刻度

转载 作者:行者123 更新时间:2023-12-01 08:26:10 25 4
gpt4 key购买 nike

我在 tkinter 中嵌入了两个不同的 matplotlib 图形,我使用按钮在它们之间进行切换。第一个图形切换周期按预期绘制图形,但此后每次在两个图形之间切换都会导致第一个图形具有意外的 x 和 y 轴标签集以及范围从 0 到 1 的刻度。所需的图形一个 x 和 y 轴标签和刻度按预期出现,但此外,额外的 0 到 1 范围标签/刻度覆盖在预期标签/刻度上。图表的两个 x 和 y 轴标签/刻度表现正常,并且没有意外的重叠。以下是我的代码的简化版本,后面是一些显示问题的图表图片。

import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
from tkinter import *
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,
NavigationToolbar2Tk)
from matplotlib.backend_bases import key_press_handler
import numpy as np

# Separated out config of plot to just do it once
def config_plot():
fig = plt.figure(figsize=(18, 5))
return (fig)



class graphSwitcher:
def __init__(self, master):
self.master = master
self.frame = Frame(self.master)
self.fig = config_plot()
self.graphIndex = 0
self.canvas = FigureCanvasTkAgg(self.fig, self.master)
self.config_window()
self.graph_one(self.fig)
self.frame.pack(expand=YES, fill=BOTH)


def config_window(self):
self.canvas.mpl_connect("key_press_event", self.on_key_press)
toolbar = NavigationToolbar2Tk(self.canvas, self.master)
toolbar.update()
self.canvas.get_tk_widget().pack(side=TOP, fill=BOTH,
expand=1)
self.button = Button(self.master, text="Quit",
command=self._quit)
self.button.pack(side=BOTTOM)
self.button_switch = Button(self.master, text="Switch Graphs",
command=self.switch_graphs)
self.button_switch.pack(side=BOTTOM)
plt.subplots_adjust(bottom=0.2)


# the def creates the first matplotlib graph
def graph_one(self, fig):
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

self.ax = fig.subplots()
try:
self.ax1.clear() # clear current axes
self.ax2.clear()
except AttributeError:
pass

self.ax.plot(t, s)

self.ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='Graph One')

self.canvas.draw()

# This def creates the second matplotlib graph that uses subplot
# to place two graphs one on top of the other
def graph_two(self, fig):
x1 = np.linspace(0.0, 5.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
self.ax1 = plt.subplot2grid((5, 4), (0, 0), rowspan=4,
colspan=4)
self.ax1.plot(x1, y1, 'o-')
self.ax1.set(title='Graph Two')

means_men = (20, 35, 30, 35, 27)
std_men = (2, 3, 4, 1, 2)
self.ax2 = plt.subplot2grid((5, 4), (4, 0), sharex=self.ax1,
rowspan=1, colspan=4)
self.ax2.bar(std_men, means_men, color='green', width=0.5,
align='center')

self.canvas.draw()

def on_key_press(event):
key_press_handler(event, canvas, toolbar)

def _quit(self):
self.master.quit() # stops mainloop

def switch_graphs(self):
self.graphIndex = (self.graphIndex + 1 ) % 2
if self.graphIndex == 0:
self.graph_one(self.fig)
else:
self.graph_two(self.fig)



def main():
root = Tk()
graphSwitcher(root)
root.mainloop()

if __name__ == '__main__':
main()

以下是按顺序看到的图表的图片,显示前两次看到各个图表(周期 1)时,图表具有正确的轴标签和刻度。但第二次显示第一个图时,出现了一组额外的意外 x 和 y 轴标签和刻度。最后两张图在每个周期重复(按两次按钮) Cycle 1 Graph 1

Cycle 1 Graph 2

Cycle 2 Graph 1

Cycle 2 Graph 2

如果有人对如何消除图一上出现的意外 x 和 y 轴标签/刻度有任何想法,我将不胜感激。

最佳答案

您正在清除在调用graph_two时创建的轴,但您并没有删除它们。因此,当您第二次绘制第一个图表时,graph_two 轴仍然位于 graph_one 轴下方。您看不到这两个子图本身,因为它们位于下面,但轴刻度标签确实显示在边缘,这就是您所看到的。

如果您在 graph_one 中删除绘图和图形创建,则更容易看到这一点,您将能够看到 2 个子图被清除,但按下按钮时并未删除。

因此解决方案很简单,使用axes.remove反而。所以 graph_one 看起来像:

def graph_one(self, fig):
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

self.ax = fig.subplots()
try:
self.ax1.remove() # remove current axes
self.ax2.remove()
except AttributeError:
pass

self.ax.plot(t, s)

self.ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='Graph One')

self.canvas.draw()

关于python - 当在 tkinter 中嵌入的两个 matplotlib 图形之间切换时,第一个图形上会出现一组额外的意外 x 轴标签和刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54239757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com