我的 Matplotlib 子图标题会自行删除



当我运行代码时,我创建了一个图形,然后在该图形中创建了一个子绘图。然后,当我尝试使用ax.set_title("title")为其添加标题时,它有时会在一瞬间显示然后消失。我也尝试使用plot.title但没有运气。

我尝试在一个小示例中重新创建错误,但由于某种原因,它在那里工作得很好,所以这里是代码的整个源代码。

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.style as style
import plotgen
from matplotlib.widgets import Button
class plotWindow():
    def __init__(self):
        style.use("bmh")
        self.dp = 30
        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(1, 1, 1, label="ax1")

        self.cax = 1
        self.maxax = 2
        self.minax = 1
        plotgen.clear("plot1.txt")
        plotgen.clear("plot2.txt")
        axnext = plt.axes([0.80, 0.01, 0.06, 0.06])
        axprev = plt.axes([0.73, 0.01, 0.06, 0.06])
        bnext = Button(axnext, 'Next >')
        bnext.on_clicked(self.changePlotNext)
        bprev = Button(axprev, "< Previous")
        bprev.on_clicked(self.changePlotPrev)

        ani = animation.FuncAnimation(self.fig, self.animate, interval=500)
        plt.show()
    def changePlotNext(self, i):
        if self.cax < self.maxax:
            self.cax += 1
            self.ax.set_title("Pump " + str(self.cax))
    def changePlotPrev(self, i):
        if self.cax > self.minax:
            self.cax -= 1
            self.ax.set_title("Pump " + str(self.cax))
    def animate(self, i):
        if self.cax == 1:
            plotgen.generate("plot1.txt")
            graph_data = open('plot1.txt', 'r').read()
            lines = graph_data.split('n')
            xs = []
            ys = []
            for line in lines:
                if len(line) > 1:
                    x, y = line.split(',')
                    xs.append(x)
                    ys.append(float(y))
            self.ax.clear()
            lx = len(xs)
            ly = len(ys)
            if len(xs) < self.dp:
                pxs = xs
                pys = ys
            else:
                pxs = xs[(lx - (self.dp - 1)):(lx - 1)]
                pys = ys[(ly - (self.dp - 1)):(ly - 1)]
            self.ax.plot(pxs, pys, "r")
        elif self.cax == 2:
            plotgen.generate("plot2.txt")
            graph_data = open('plot2.txt', 'r').read()
            lines = graph_data.split('n')
            xs = []
            ys = []
            for line in lines:
                if len(line) > 1:
                    x, y = line.split(',')
                    xs.append(x)
                    ys.append(float(y))
            self.ax.clear()
            lx = len(xs)
            ly = len(ys)
            if len(xs) <= self.dp:
                pxs = xs
                pys = ys
            else:
                pxs = xs[(lx - (self.dp - 1)):(lx - 1)]
                pys = ys[(ly - (self.dp - 1)):(ly - 1)]
            self.ax.plot(pxs, pys)
plotWindow()

正如您在我的changePlotNextchangePlotPrev函数中看到的那样,我正在尝试更改标题。有时,当我更改时,它们会显示一瞬间,但随后它就会消失。而且我很清楚,在我改变情节之前,我没有设置要显示的标题。

animate 中,你有 self.ax.clear() ,它删除了轴上的所有艺术家、文本等,包括标题。

那么,一个简单的选择是在清除轴后重置标题。因此,如果您添加:

 self.ax.set_title("Pump " + str(self.cax))

在这两个位置,在您拨打self.ax.clear()后,您的标题仍将显示。

另一种选择是停止清除轴,但只需删除需要删除的项目。我想这只是你画的线?因此,例如,您可以删除对self.ax.clear()的调用,并添加:

for line in self.ax.lines:
    line.remove()

取而代之。这将仅删除绘制的线,但保留标题。

最新更新