0

I'm trying to create a "program" that allows me to have 2 seperate plots, in 1 window. I don't want them to be displayed at the same time, I want to be able to switch between the plots using the arrows at the bottom of the figure window.

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.style as style
import plotgen

style.use("bmh")
dp = 30

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1, label="ax1")
ax2 = fig.add_subplot(1,1,1, label="ax2")


cax = 2

plotgen.clear("plot1.txt")
plotgen.clear("plot2.txt")

def animate(i):
    if 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))
        ax1.clear()
        pxs = 0
        pys = 0
        lx = len(xs)
        ly = len(ys)
        if len(xs) < dp:
            pxs = xs
            pys = ys
        else:
            pxs = xs[(lx - (dp - 1)):(lx - 1)]
            pys = ys[(ly - (dp - 1)):(ly - 1)]
        ax1.plot(pxs, pys, "r")
    elif 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))
        ax2.clear()
        pxs = 0
        pys = 0
        lx = len(xs)
        ly = len(ys)
        if len(xs) < dp:
            pxs = xs
            pys = ys
        else:
            pxs = xs[(lx - (dp - 1)):(lx - 1)]
            pys = ys[(ly - (dp - 1)):(ly - 1)]
        ax2.plot(pxs, pys)

ani = animation.FuncAnimation(fig, animate, interval=500)
plt.show()

At the moment I am currently switching between what plot I want to see on the figure using the cax variable, this is the one I want to increase or decrease using the arrows.

Also for some reason I can't see the ax1 subplot, I think it's "behind" ax2. The data generating works for both though.

I just wanted to clarify what I wanted the answer for:

1: "Is it possible to use the arrows to switch between plots?"

2: "Is it possible to make one plot invisible when it's plotting the other (remove x and y axis aswell so they don't mix)"

3: "Why can't I see ax1?"

Franix
  • 103
  • 8
  • (1) Possible, but very hard. (2) `ax.set_visible(False)` would make `ax` invisible. (3) You cannot see `ax1`, because `ax2` is on top of it. – ImportanceOfBeingErnest Apr 04 '19 at 12:21
  • Thanks, is there an event happening when I click it? If so I can just use something like `fig.canvas.mpl_connect('next_arrow_event', next)` – Franix Apr 04 '19 at 12:28
  • It's not that simple. What about adding your own buttons, like in [here](https://stackoverflow.com/questions/44985966/managing-dynamic-plotting-in-matplotlib-animation-module) or [here](https://stackoverflow.com/a/41152160/4124317)? – ImportanceOfBeingErnest Apr 04 '19 at 12:36

1 Answers1

0

I fixed the issue I had with ax1 not showing by using ax2.set_visible(False). And when I changed the plots I made it visible again and made ax1 invisible.

I also added buttons to my plot using this short piece of code:

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)

Then I just added simple functions that would increase or decrease the cax variable I have. I had to do some minor edits to my animate function to save resources when plotting, and I also deleted the second subplot and decided to just have one instead.

Franix
  • 103
  • 8