Matplotlib 为什么单击后绘图函数会在按钮内绘制?
Matplotlib why does the plot function draw inside the button after i click?
你好,我想在单击按钮事件后在我的绘图中绘制图形,问题是图形是在按钮内绘制的,而不是普通绘图。
代码如下:
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
在当前轴上绘制。因此,当您为按钮创建轴时,它成为当前轴,然后您尝试绘制,它绘制在那里而不是您的主轴。
如果你切换到 object-oriented interface,这个问题就会消失:
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()
你好,我想在单击按钮事件后在我的绘图中绘制图形,问题是图形是在按钮内绘制的,而不是普通绘图。
代码如下:
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
在当前轴上绘制。因此,当您为按钮创建轴时,它成为当前轴,然后您尝试绘制,它绘制在那里而不是您的主轴。
如果你切换到 object-oriented interface,这个问题就会消失:
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()