简体   繁体   中英

My Matplotlib subplot title removes itself

When I run my code I create a figure, then I create a subplot in that figure. Then when I try to add a title to it using ax.set_title("title") it sometimes shows up for a split second then goes away. I have tried using plot.title aswell with no luck.

I tried recreating the error in a small example but for some reason it worked just fine there, so here is the entire source code of the code.

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

As you can see in my changePlotNext and changePlotPrev functions I'm trying to change the title. Sometimes they display for a split second when I change, but then it goes away. And I am very aware that I have not set a title to display before I change the plot.

In animate , you have self.ax.clear() , which is removing all artists, texts, etc. on the Axes, including the title.

A simple option, then, is to reset the title after you clear the axes. So if you add:

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

in both places, immediately after you call self.ax.clear() , your titles will still be shown.

Another option would be to stop clearing the axes, but just remove the items you need to remove. I think this is just the lines you have plotted? So, for example, you could remove the call to self.ax.clear() , and add:

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

in its place. That will remove just the plotted line, but retain the title.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM