代码之家  ›  专栏  ›  技术社区  ›  Sundar N

如何创建具有可滚动X轴和单个滑块的多个绘图

  •  0
  • Sundar N  · 技术社区  · 7 年前

    我打算创建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轴)。

    2 回复  |  直到 7 年前
        1
  •  0
  •   Sundar N    7 年前

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

    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))
    
        2
  •  0
  •   Sundar N    7 年前

    下面的实现片段。

    1. 加载所有图形,仅使用带有滑块功能的plt.xlim(最小值x,最大值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
          );