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
?"