hi我试图在按钮点击事件后在绘图中绘制一个图形,问题是该图形是在按钮内部绘制的,而不是正常的绘图。
代码在这里:
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider,Button
class Details:
def drawPlot(self,event):
plt.plot([1,3,4])
def addTitle(self,title):
plt.title(title)
def plot(self):
x = 1
plt.show()
def addButton(self):
self.axnext = plt.axes([0.71, 0.1, 0.1, 0.075])
self.bnext = Button(self.axnext, 'Next')
self.bnext.on_clicked(self.drawPlot)
d = Details()
d.addTitle("cas dakar 1")
d.addButton()
d.plot()
提前感谢!
plt.plot
在当前轴上绘制。因此,当您为按钮创建轴时,它将成为当前轴,然后您尝试绘制,它将在那里绘制,而不是您的主轴。
如果你切换到面向对象的界面,这个问题就会消失:
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider,Button
class Details:
def __init__(self):
fig, ax = plt.subplots()
self.fig = fig
self.ax = ax
def drawPlot(self,event):
self.ax.plot([1,3,4])
def addTitle(self,title):
self.ax.set_title(title)
def plot(self):
x = 1
plt.show()
def addButton(self):
self.axnext = plt.axes([0.71, 0.1, 0.1, 0.075])
self.bnext = Button(self.axnext, 'Next')
self.bnext.on_clicked(self.drawPlot)
d = Details()
d.addTitle("cas dakar 1")
d.addButton()
d.plot()