簡體   English   中英

Bqplot在動畫中的每個步驟渲染新條形圖,而不是移動原始條形圖

[英]Bqplot rendering new bar at each step in animation instead of moving the original bar

我正在嘗試為Bqplot中的條形圖設置動畫。 我希望能夠更新條形的x和y值,並讓它們順利調整。 下面的代碼對於我的圖表中的3個條形中的2個按預期運行,但是在每個時間步長處完全重繪第3個條形。

我最初只用2個酒吧來嘗試這個。 第一個欄表現符合預期,但第二個欄將在每個時間步重新繪制。 我添加了第3個欄來嘗試解決問題。

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

當您順利地說'調整'時,您希望條形圖向右移動,而不重繪? 我不認為bqplot'移動'那樣的吧。 新條1和2的數據已經存在(因為它們被繪制為舊條2和3),因此這些不會重繪。 之前不存在新的條形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])

這是一種在x中很好地制作“條形圖”的方法。 它不能用酒吧來完成。 相反,我使用了線標記 - 這是一個黑客。 但是你獲得了靈活性。 我更喜歡在對象模型中使用Bqplot(而不是pyplot方式)。

在此輸入圖像描述

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM