Bqplot在动画的每个步骤渲染新的条形,而不是移动原始条形

时间:2019-04-02 21:00:01

标签: python bqplot

我正在为Bqplot中的条形图制作动画。我希望能够同时更新条形图的x和y值,并使它们平滑调整。 下面的代码的行为与图表中3条柱中的2条的预期相同,但第3条柱在每个时间步均被完全重绘。

我最初只用2条尝试。第一个柱的行为符合预期,但是第二个柱将在每个时间步重绘。我添加了第三个栏以尝试解决问题。

initialIndex = 0
idxSlider3 = IntSlider(min=0, max=20, step=1, 
    description='Index',value=initialIndex)

''' I want to update the bar chart with this function '''
def update_chart3(change):
    with bar3.hold_sync():
        bar3.x = [idxSlider3.value, 
             idxSlider3.value + 1, 
             idxSlider3.value + 2]
        bar3.y = [idxSlider3.value, idxSlider3.value, idxSlider3.value] 

idxSlider3.observe(update_chart3, 'value')  

fig3 = plt.figure(animation_duration=1000)

bar3 = plt.bar(x=[0, 1, 2], y=[1, 1, 0])

play_button3 = Play(min=0, max=20, step=1, interval=1000, initial_value=0)
jslink((play_button3, 'value'), (idxSlider3, 'value'))

VBox([HBox([idxSlider3, play_button3]), fig3])

2 个答案:

答案 0 :(得分:0)

当您平稳地说“调整”时,您希望条形图向右移动而不重画吗?我不认为bqplot会像这样“移动”酒吧。新条1和2的数据已经存在(因为它们被绘制为旧条2和3),因此它们不会重绘。新的bar 3数据以前不存在,这需要从头开始绘制。这样会出现“移动”条的外观。

作为另一个示例,请注意将条形图移动一个单位但将它们分开两个个单位时会发生什么。您应该每次都看到所有条从头开始绘制。

from ipywidgets import *
import bqplot.pyplot as plt

initialIndex = 0
idxSlider3 = IntSlider(min=0, max=20, step=1, 
    description='Index',value=initialIndex)

''' I want to update the bar chart with this function '''
def update_chart3(change):
    with bar3.hold_sync():
        bar3.x = [idxSlider3.value, 
             idxSlider3.value + 2, 
             idxSlider3.value + 4]
        bar3.y = [idxSlider3.value, idxSlider3.value, idxSlider3.value] 

idxSlider3.observe(update_chart3, 'value')  

fig3 = plt.figure(animation_duration=1000)

bar3 = plt.bar(x=[0, 1, 2], y=[1, 1, 0])

play_button3 = Play(min=1, max=20, step=1, interval=1000, initial_value=0)
jslink((play_button3, 'value'), (idxSlider3, 'value'))

VBox([HBox([idxSlider3, play_button3]), fig3])

答案 1 :(得分:0)

这是在x中很好地制作“条”动画的一种方法。用酒吧不能做到。相反,我使用了线标记-这是一个技巧。但是您可以获得灵活性。而且我更喜欢在对象模型中使用Bqplot(而不是pyplot方式)。

enter image description here

import bqplot as bq
from ipywidgets import *
import numpy as np

initialIndex = 0
xBar=[0, 2, 4] 
yBar=[1, 1, 0]
zeroY = 0
barWidth = 1

idxSlider3 = IntSlider(min=0, max=20, step=1, 
    description='Index',value=initialIndex)

''' I want to update the bar chart with this function '''
def update_chart3(change):
    xBar = [idxSlider3.value, 
         idxSlider3.value + 2, 
         idxSlider3.value + 4]
    np.random.rand()
    yBar = [idxSlider3.value + np.random.rand()*2, idxSlider3.value + np.random.rand()*2, idxSlider3.value + np.random.rand()*2] 
    xList, yList = getLineCoordsFromBarLike(xBar, yBar)
    lines.x = xList
    lines.y = yList

idxSlider3.observe(update_chart3, 'value')  

x_sc = bq.LinearScale()
y_sc = bq.LinearScale()

x_ax1 = bq.Axis(label='', scale=x_sc, num_ticks = 3)
y_ax1 = bq.Axis(label='', scale=y_sc, orientation='vertical')

def getLineCoordsFromBarLike(xBar, yBar):
    """ Convert of single list of bar coordinates to a 2D array of line coordinates that describe the rectangular shape

    xBar: 1D list of numbers for central x position of bar chart
    yBar: 1D list of numbers for amplitudes of bar chart  (must be same length as xBar)

    retrns x cordinates 2D array, y coordinates 2D array.   
    """
    xList= []
    yList= []
    for num, x in enumerate(xBar):
        y = yBar[num] 
        xList.append([x - barWidth/2, x - barWidth/2, x + barWidth/2, x + barWidth/2,])
        yList.append([zeroY, y, y, zeroY])
    x_ax1.tick_values = xBar 
    return xList, yList

xList, yList = getLineCoordsFromBarLike(xBar, yBar)   

lines = bq.Lines(x=xList, y=yList,
    scales={'x': x_sc, 'y': y_sc},
    colors=['black'], 
    display_legend=True, 
#     tooltip=def_tt,
    stroke_width = .5,
     close_path = True,
     fill = 'inside',
    fill_colors = bq.colorschemes.CATEGORY10 * 10
    )

margins = dict(top=10, bottom=40, left=50, right=30)
fig3 = bq.Figure(marks=[lines], axes=[x_ax1, y_ax1], fig_margin=margins, animation_duration=1000)
fig3.layout.width = '600px'
fig3.layout.height = '600px'

# def_tt = bq.Tooltip(fields=['name',], formats=['',],  labels=['id', ])
play_button3 = Play(min=1, max=20, step=1, interval=1000, initial_value=0)
jslink((play_button3, 'value'), (idxSlider3, 'value'))

VBox([HBox([idxSlider3, play_button3]), fig3])