简体   繁体   中英

Overlay plots and scroll independently matplotlib

The problem I have can be described as follows: Two different datasets with two different x and y axes (lets call them t1,y1,t2, and y2), t1 and t2 can be the same.

What I need to do is to overlay/plot both plots together (ie, not in subplots, or in subplots that are the same size and exactly overlap one another) and be able to scroll each axis independently. My goal is to be able to visually line them up to I can compare them.

What I have until not is the following:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.widgets import Slider
dArray = np.genfromtxt("t5_24.csv",delimiter=',');
y1 = dArray[:,2];
y2 = dArray[:,3];
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)
t = np.linspace(0,len(temp1),len(temp1))
p1 = plt.plot(t,y1,t,y2)


axcolor = 'lightgoldenrodyellow'
axpos = plt.axes([0.2, 0.1, 0.65, 0.03], axisbg=axcolor)

spos = Slider(axpos, 'Pos', 0.1, len(t))

def update(val):
    pos = spos.val
#    ax.xlim(pos,pos+30*60)
    ax.axis([pos,pos+120*60,0,500])
    fig.canvas.draw_idle()

spos.on_changed(update)

plt.show()

which was taken from this stackoverflow post

Essentially what I need to do (I think) is to have two axes, completely overlapping, and with two scrollbars, on the same figure.

Any help is greatly appreciated.

Sorry for any English mistakes, ESL

Here's a basic example I can get working with two random datasets where you can vary the x-axis position of the two datasets independently on the same plot.

import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

t = np.linspace(0, 10, 101)
y1, y2 = np.random.rand(2, 101)

fig, ax1 = plt.subplots()
ax2 = ax1.twiny()

fig.subplots_adjust(bottom=0.25)

ax1_pos = fig.add_axes([0.2, 0.1, 0.65, 0.03])
ax2_pos = fig.add_axes([0.2, 0.05, 0.65, 0.03])

s1 = Slider(ax1_pos, 'Pos1', 0.1, len(x))
s2 = Slider(ax2_pos, 'Pos2', 0.1, len(x))

def update1(v):
    pos = s1.val
    ax1.axis([pos,pos+2,0,1])
    fig.canvas.draw_idle()

def update2(v):
    pos = s2.val
    ax2.axis([pos,pos+2,0,1])
    fig.canvas.draw_idle()

s1.on_changed(update1)
s2.on_changed(update2)

ax1.plot(t, y1, 'b-')
ax2.plot(t, y2, 'r-')
plt.show()

This results in the following: 在此输入图像描述

You will likely need to change the update functions to fit your actual data (mine are different than the one listed in the OP).

If you are instead interested in the having the same x-axis values but would like to vary the y-axis positions of each plot independently, you can use ax2 = ax1.twiny() and change the update functions accordingly (something like ax1.axis([xmin, xmax, pos, pos+2]) ).

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