简体   繁体   English

叠加图并独立滚动matplotlib

[英]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. 我的问题可以描述如下:两个不同的数据集有两个不同的x和y轴(让我们称之为t1,y1,t2和y2),t1和t2可以是相同的。

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 这是从这个stackoverflow帖子中获取的

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 对不起任何英语错误,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. 这是一个基本的例子,我可以使用两个随机数据集,您可以在同一个图上独立地改变两个数据集的x轴位置。

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). 您可能需要更改update功能以适合您的实际数据(我的不同于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]) ). 如果您对具有相同的x轴值感兴趣但想要独立地改变每个图的y轴位置,则可以使用ax2 = ax1.twiny()并相应地更改更新函数(类似于ax1.axis([xmin, xmax, pos, pos+2]) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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