【问题标题】:Screen flickering with matplotlib slider update屏幕闪烁与 matplotlib 滑块更新
【发布时间】:2018-11-17 07:04:25
【问题描述】:

已尝试使用公共滑块在共享 x 轴上实现多个绘图。在滑块更新时,屏幕闪烁过多。这怎么可能避免。这是我使用的代码示例。

%matplotlib inline
from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np

''' 30% window size on the selected time on slider'''
data_size=round(M.Timestamp.size*0.30)
plt.close('all')

def f(m):
    plt.figure(2)
    x=M['Timestamp']
    y1=M['Value']
    '''define boundary limits for both axis'''
    min_x=0 if m-data_size < 0 else m-data_size
    max_x=M.Timestamp.size if m+data_size > M.Timestamp.size else m+data_size

    f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=True)
    ax1.plot(x[min_x:max_x],y1[min_x:max_x],color='r')
    ax1.set_title('Sharing both axes')
    ax2.plot(x[min_x:max_x],y1[min_x:max_x],color='b')
    ax3.plot(x[min_x:max_x],y1[min_x:max_x],color='g')
    plt.xticks(rotation=30)

interactive(f, m=(0, M.Timestamp.size))

当尝试更新滑块移动时的 xlimit 时,图表是空白的,因此使用数据子集来更新图表

【问题讨论】:

  • 你能提供一些虚拟数据吗?你没有定义你的数据集M

标签: python-3.x matplotlib jupyter-notebook ipywidgets


【解决方案1】:

通过以下设置解决了问题。

  1. 使用了选择滑块和 Continuous_update =False
  2. 在启动时加载图表并仅使用带有滑块功能的 plt.xlim(min_x,max_x) 操作 xlim

sn-p 下面的实现。

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
    );

【讨论】:

  • 你有一个可运行的例子我可以复制粘贴来测试吗?我正在努力让 pyplot 更新绘图而不闪烁三遍。
猜你喜欢
  • 1970-01-01
  • 2015-10-19
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-06
  • 2015-11-05
相关资源
最近更新 更多