Python Plotly函数创建一个新图形,而不是更新现有图形

时间:2019-05-02 16:55:54

标签: python plot plotly figure

我绘制了一个空图,并尝试使用ipywidgets下拉菜单对其进行更新。但是,每次更改菜单值时,都会创建一个新图形,而不是更新图形。

代码如下:

coeff_options = ['','Pearson','Spearman','Kendall']
year_options = ['','2019','2018']

coeff = widgets.Dropdown(
    description='Coefficient',
    value= '',
    options=coeff_options
)

year = widgets.Dropdown(
    description='Year',
    value= '',
    options=year_options
)

data = []

fig = go.Figure(data=data)
iplot(fig)

container2 = widgets.HBox([coeff, year])
display(container2)

def response(change):
    global fig

    if coeff.value=='Pearson' and year.value=='2019' :
        pearson19_heatmap = go.Heatmap(z=[pearson19['btc'],pearson19['eth'],pearson19['xrp'],pearson19['ltc'],
                                  pearson19['dash'],pearson19['xmr'],pearson19['rdd']],
                    x=pearson19.columns,y=pearson19.columns)
        pearson19_data = [pearson19_heatmap]
        fig = go.Figure(data=pearson19_data)
        iplot(fig)

    elif coeff.value=='Pearson' and year.value=='2018' :
        pearson18_heatmap = go.Heatmap(z=[pearson18['btc'],pearson18['eth'],pearson18['xrp'],pearson18['ltc'],
                                  pearson18['dash'],pearson18['xmr'],pearson18['rdd']],
                    x=pearson18.columns,y=pearson18.columns)
        pearson18_data=[pearson18_heatmap]
        fig = go.Figure(data=pearson18_data)
        iplot(fig)



coeff.observe(response, names="value")
year.observe(response, names="value")

我们将不胜感激。

谢谢

0 个答案:

没有答案