首页 > 解决方案 > 我的 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函数中看到的那样,我正在尝试更改标题。有时,当我更改时,它们会显示一秒钟,但随后就消失了。而且我非常清楚在更改情节之前我没有设置要显示的标题。

标签: pythonmatplotlib

解决方案


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()

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


推荐阅读