我的Matplotlib子图标题自行删除

问题描述 投票:-1回答:1

当我运行我的代码时,我创建了一个图,然后我在该图中创建了一个子图。然后,当我尝试使用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函数中看到的那样,我正试图改变标题。有时它们会在我改变时显示一瞬间,但随后它会消失。并且我非常清楚在更改绘图之前我没有设置要显示的标题。

python matplotlib
1个回答
1
投票

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

因此,一个简单的选项是在清除轴后重置标题。所以,如果你添加:

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

在你调用self.ax.clear()之后,两个地方都会立即显示你的标题。

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

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

在它的位置。这将仅删除绘制的线条,但保留标题。

© www.soinside.com 2019 - 2024. All rights reserved.