如何使用滑块自动更新与数据框变化的图表。

huangapple go评论70阅读模式
英文:

How to update automatically a plot from a dataframe that varies with a slider

问题

I am trying to create a dashboard with panel. I have a float slider linked to a function that outputs a dataframe. Which means the dataframe varies with a slider. Then from this dataframe, I create a line plot. When I "interact" the slider with the function and display the plot, only the dataframe updates but the plot doesn't refresh. I am new to python and even more to panel and widgets, so maybe I am trying to do something way too complicated that I could do easily in another way.

import numpy as np
import pandas as pd
import panel as pn
from panel.interact import interact

slider = pn.widgets.FloatSlider(name='Slider', start=0, end=70, value=0, width=400)

def test(V):
    x = np.linspace(0, 85, 850)
    y1 = []
    y = []
    for k in x:
        y1.append(np.cos(0.10995574287564276 - 0.00022913079470198672 * k**2))
    for k in y1:
        y.append((15 + V * 0.05) * k)

    df = pd.DataFrame(list(zip(x, y1, y)), columns=['X', 'Intermediate result', 'Y'])

    return df

inter = interact(test, V=slider)
plot = test(slider.value).hvplot(x='X', y='Y')
pn.Row(pn.Column(inter), pn.Column(plot))

Couldn't manage to update the plot; when I just put the slider as a parameter, it won't run, and when I put slider.value, it only takes the first dataframe and never updates it.

英文:

I am trying to create a dashboard with panel. I have a float slider linked to a function that outputs a dataframe. Which means the dataframe varies with a slider. Then from this dataframe i create a line plot.
When i "interact" the slider with the function and display the plot, only the dataframe updates but the plot doesn't refresh. I am new to python and even more to panel and widgets, so maybe i am trying to do something way too complicated that i could do easily in another way.


    import numpy as np
    import pandas as pd
    import panel as pn
    from panel.interact import interact
    
    
slider=pn.widgets.FloatSlider(name='Slider',start=0, end=70, value=0, width=400)
    
def test(V):
    x=np.linspace(0,85,850)
    y1=[]
    y=[]
    for k in x:
        y1.append(np.cos( 0.10995574287564276 - 0.00022913079470198672 * k**2 ))
    for k in y1:
        y.append((15 + V * 0.05) * k)
    
    df=pd.DataFrame(list(zip(x, y1, y)), columns = ['X' , 'Intermediate result' , 'Y'])
    
    return df    
    
inter=interact(test, V = slider)
plot=test(slider.value).hvplot(x='X', y='Y')
pn.Row(pn.Column(inter),
pn.Column(plot))

Couldn't manage to update the plot, when i just pu the slider as a parameter, it won't run and when i put slider.value, it only takes the first dataframe and never updates it

答案1

得分: 1

The interact API提供的功能由Panel提供,与IPywidgets类似,适用于非常简单的情况,文档通常会引导您使用其他更灵活但简单易用的API。特别是在您的示例中,您希望在小部件更改时更新两个输出,这种情况下,interact并不是最佳选项(除非该函数返回包含两个输出的布局,但这并不是interact的真正用途)。

相反,您可以使用pn.bind(来自Panel)或hvplot.interactive(来自hvPlot)。

使用pn.bind的示例:

import hvplot.pandas
import numpy as np
import pandas as pd
import panel as pn

pn.extension()
    
slider=pn.widgets.FloatSlider(name='Slider',start=0, end=70, value=0, width=400)
    
def compute_df(V):
    x=np.linspace(0,85,850)
    y1=[]
    y=[]
    for k in x:
        y1.append(np.cos( 0.10995574287564276 - 0.00022913079470198672 * k**2 ))
    for k in y1:
        y.append((15 + V * 0.05) * k)
    
    df=pd.DataFrame(list(zip(x, y1, y)), columns = ['X' , 'Intermediate result' , 'Y'])
    
    return df    

def compute_plot(df):
    return df.hvplot(x='X', y='Y')

pn.Row(
    pn.Column(slider, pn.bind(compute_df, slider), height=400),
    pn.bind(compute_plot, pn.bind(compute_df, slider))
).servable()

使用hvplot.interactive的示例:

import hvplot.pandas
import numpy as np
import pandas as pd
import panel as pn

pn.extension()
    
slider=pn.widgets.FloatSlider(name='Slider',start=0, end=70, value=0, width=400)
    
def compute_df(V):
    x=np.linspace(0,85,850)
    y1=[]
    y=[]
    for k in x:
        y1.append(np.cos( 0.10995574287564276 - 0.00022913079470198672 * k**2 ))
    for k in y1:
        y.append((15 + V * 0.05) * k)
    
    df=pd.DataFrame(list(zip(x, y1, y)), columns = ['X' , 'Intermediate result' , 'Y'])
    
    return df    

dfi = hvplot.bind(compute_df, slider).interactive(height=400)

pn.Row(dfi, dfi.hvplot(x='X', y='Y').output()).servable()

这两个示例生成的应用程序完全相同,您可以在笔记本中显示它们,或使用panel serve filename.py进行部署。

英文:

The interact API offered by Panel - and modeled on IPywidgets - is suitable for very simple cases, the docs generally steer you towards using other more flexible, yet simple to use, API. In your example in particular you want two outputs to be updated on a widget change, in which case interact isn't the best option (unless the function returns a layout consisting of the two outputs, but that's not really the spirit of interact).

Instead you can use pn.bind (from Panel) or hvplot.interactive (from hvPlot).

With pn.bind:

import hvplot.pandas
import numpy as np
import pandas as pd
import panel as pn

pn.extension()
    
slider=pn.widgets.FloatSlider(name='Slider',start=0, end=70, value=0, width=400)
    
def compute_df(V):
    x=np.linspace(0,85,850)
    y1=[]
    y=[]
    for k in x:
        y1.append(np.cos( 0.10995574287564276 - 0.00022913079470198672 * k**2 ))
    for k in y1:
        y.append((15 + V * 0.05) * k)
    
    df=pd.DataFrame(list(zip(x, y1, y)), columns = ['X' , 'Intermediate result' , 'Y'])
    
    return df    

def compute_plot(df):
    return df.hvplot(x='X', y='Y')

pn.Row(
    pn.Column(slider, pn.bind(compute_df, slider), height=400),
    pn.bind(compute_plot, pn.bind(compute_df, slider))
).servable()

With hvplot.interactive:

import hvplot.pandas
import numpy as np
import pandas as pd
import panel as pn

pn.extension()
    
slider=pn.widgets.FloatSlider(name='Slider',start=0, end=70, value=0, width=400)
    
def compute_df(V):
    x=np.linspace(0,85,850)
    y1=[]
    y=[]
    for k in x:
        y1.append(np.cos( 0.10995574287564276 - 0.00022913079470198672 * k**2 ))
    for k in y1:
        y.append((15 + V * 0.05) * k)
    
    df=pd.DataFrame(list(zip(x, y1, y)), columns = ['X' , 'Intermediate result' , 'Y'])
    
    return df    

dfi = hvplot.bind(compute_df, slider).interactive(height=400)

pn.Row(dfi, dfi.hvplot(x='X', y='Y').output()).servable()

These two examples result exactly in the same app that you can display in a notebook or serve with panel serve filename.py:

如何使用滑块自动更新与数据框变化的图表。

答案2

得分: 0

请查看H5Gizmos。
以下教程提供了一个根据滑块数值更新的示例图表。

https://github.com/AaronWatters/H5Gizmos/blob/main/doc/Tutorials/hello_curves.md

让我知道进展如何。谢谢!

英文:

Please have a look at H5Gizmos.
The following tutorial gives an example of a plot that
updates based on slider values.

https://github.com/AaronWatters/H5Gizmos/blob/main/doc/Tutorials/hello_curves.md

Let me know how it goes. Thanks!

答案3

得分: 0

以下是翻译好的内容:

MaximeL提供的解决方案乍看起来效果不错,但是当我尝试将仪表板保存为HTML文件时出现错误,以下是我的操作步骤:

dash = pn.Row(dfi, dfi.hvplot(x='X', y='Y').output())
dash.servable()
dash.save(filename="dashboard")

这是返回的错误信息:

警告: bokeh.core.validation.check:W-1005 (FIXED_SIZING_MODE): 'fixed' 缩放模式要求设置宽度和高度:Row(id='2590', ...)

我注意到我可以跳过这个错误并创建面板仪表板。在那一刻,当我打开HTML文件时,一切看起来都很好,但滑块不再更新图表。我在GitHub上看到了这个错误,但我无法理解如何解决它,甚至不确定是否能解决。我从未固定过宽度或高度,但我假设这是默认参数,因此在更改缩放模式或固定高度和长度时仍然出现此错误。也许以另一种方式保存为HTML文件或显示仪表板可以解决这个问题。

英文:

The solution MaximeL brought works well at first sight, but i have an error when trying to save the dashboard as a html file: here is how i proceed:

dash = pn.Row(dfi, dfi.hvplot(x='X', y='Y').output())
dash.servable()


dash.save(filename = "dashboard")

And this is what returns.

> WARNING:bokeh.core.validation.check:W-1005 (FIXED_SIZING_MODE): 'fixed' sizing mode requires width and height to be set: Row(id='2590', ...)

I saw I could skip on error and it creates the panel dashboard. At that moment when I open the html file everything looks fine but the slider does not update anymore the plot.
I saw on github this error but I couldn't manage to understand how to solve it or even if I can solve it.
I never fixed the width or height but I assumed it was a default parameter, so when changing the sizing mode or fixing a height and a length and still I get this error.
Maybe another way of saving it as an html file or displaying the dashboard would solve this issue.

huangapple
  • 本文由 发表于 2023年5月11日 19:25:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定