如何使用单个滑块创建具有可滚动 x 轴的多个绘图



我打算在公共时间轴上创建 2-3 个共享的图,这些图可以通过单个滑块交互式滚动。此外,还有一个约束,其中每个变量的采样频率不同,但具有相同的时间窗口。

x  - time
y1 - values sampled every 1 second
y2 - values sampled every 10 seconds
y3 - values sampled every 100 seconds 

我们怎么能做到这一点.

尝试过此示例代码

import matplotlib.pyplot as plt
from ipywidgets import interact
%matplotlib inline
def f(n):
plt.plot([0,1,2],[0,1,n])
plt.show()
interact(f,n=(0,10))

我想要类似的东西,唯一的变化是 x 和 y 轴数据是恒定的,滑块小部件用于在图形显示上具有特定时间窗口的左右滚动图形(此处为 x 轴(

部分解决了交互性位的问题。

X 轴可通过滑块移动进行滚动。

%matplotlib inline
from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np
def f(m):
plt.figure(2)
x = np.linspace(-10, 10, num=1000)
plt.plot(x,x)
#plt.plot(x, m * x)
plt.xlim(m+2, m-2)
plt.show()
interactive(f, m=(-2.0, 2.0))

下面的实现片段。

  1. 加载所有图形,并使用具有滑块功能的 plt.xlim(min_x,max_x( 仅操作 xlim

    selection_range_slider = widgets.SelectionRangeSlider(
    options=options,
    index=index,
    description='Time slider',
    orientation='horizontal',
    layout={'width': '1000px'},
    continuous_update=False
    )
    #selection_range_slider
    def print_date_range(date_range):
    print(date_range)
    plt.figure(num=None, figsize=(15, 4), dpi=80, facecolor='w', 
    edgecolor='k')
    
    min_x=date_range[0]
    max_x=date_range[1]
    ax1 = plt.subplot(311)
    plt.plot(Data_1.Timestamp,Data_1.value,'r')
    plt.setp(ax1.get_xticklabels(), fontsize=6,visible=False)
    plt.xlabel('Data_1')
    ax1.xaxis.set_label_coords(1.05, 0.5)
    
    # share x only
    ax2 = plt.subplot(312, sharex=ax1)
    plt.plot(Data_2.Timestamp,Data_2.value,'b')
    # make these tick labels invisible
    plt.setp(ax2.get_xticklabels(), visible=False)
    plt.xlabel('Data_2')
    ax2.xaxis.set_label_coords(1.05, 0.5)
    # share x and y
    ax3 = plt.subplot(313, sharex=ax1)
    plt.plot(Data_3.Timestamp,Data_3.value,'g')
    ax3.xaxis.set_label_coords(1.05, 0.5)
    #plt.xlim(0.01, 5.0)
    plt.xlim(min_x,max_x)
    plt.show()
    #plt.xlabel('Data_3')
    widgets.interact(
    print_date_range,
    date_range=selection_range_slider
    );
    

最新更新