【问题标题】:Fitting a curve while updating a plot using the "bokeh" library in python使用 python 中的“散景”库更新绘图时拟合曲线
【发布时间】:2018-03-17 00:19:40
【问题描述】:

我已经编写了一段代码来使用散景库可视化数据。我将一个简单的数据框 df 定义为:

      Y1     Y2  x_val
0   3.01   0.99      0
1   5.10   3.05      1
2   6.99   7.29      2
3   8.02  13.41      3
4  10.81  20.31      4

并使用以下代码,我可以交互地绘制 x_val 与 Y1 和 Y2 的图。

import pandas as pd
from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource, Select
from bokeh.plotting import figure


dc = {'x_val':[0,1,2,3,4], 'Y1':[3.01, 5.10, 6.99, 8.02, 10.81],
     'Y2':[0.99, 3.05, 7.29, 13.41, 20.31]}
df = pd.DataFrame(dc)

source = ColumnDataSource(data={'x': df['x_val'], 'y': df['Y1'], 'y2':df['Y2']})

plot = figure(x_axis_label = 'x', y_axis_label = 'f(x)')
plot.circle('x', 'y', source=source)


def update_plot(attr, old, new):
        y = y_select.value
        new_data = {'x': df['x_val'], 'y': df[y]}
        source.data = new_data


y_select = Select(options=['Y1', 'Y2'],value='Y1',title='y-axis data')
y_select.on_change('value', update_plot)

# Create layout and add to current document
layout = row(widgetbox(y_select), plot)

# Add layout to current document
curdoc().add_root(layout)

现在,每当我更新绘图时,我想在数据上拟合一条曲线,即,当我选择绘制 x_val 与 Y1 时,也会绘制相关的拟合曲线,并且对于另一种情况也相同,即 x_val与 Y2 相比。为此,我将上面的代码扩展为以下代码:

import pandas as pd
import numpy as np
from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource, Select
from bokeh.plotting import figure
from scipy.optimize import curve_fit


def fit_func(xdata, ydata):

    def func(x,a,c):
        return a*(x**2)+a*x+c

    y = func(xdata, 1, 1)
    popt, pcov = curve_fit(func, xdata, ydata)
    new_x = np.arange(0,10,2)
    new_y = func(new_x,*popt)

    return (new_x, new_y)


dc = {'x_val':[0,1,2,3,4], 'Y1':[3.01, 5.10, 6.99, 8.02, 10.81],
     'Y2':[0.99, 3.05, 7.29, 13.41, 20.31]}
df = pd.DataFrame(dc)

dfit = pd.DataFrame({'x': fit_func(df['x_val'].values, df['Y1'].values)[0],
                     'y': fit_func(df['x_val'].values, df['Y1'].values)[1]})

source = ColumnDataSource(data={'x': df['x_val'], 'y': df['Y1'], 'y2':df['Y2']})
source_f = ColumnDataSource(data={'x': dfit['x'], 'y': dfit['y']})

plot = figure(x_axis_label = 'x', y_axis_label = 'f(x)')
plot.circle('x', 'y', source=source)
plot.line('x', 'y', source=source_f)    

def update_plot(attr, old, new):
        y = y_select.value
        new_data = {'x': df['x_val'], 'y': df[y]}
        source.data = new_data

        answ = fit_func(df['x_val'].values, df[y].values)
        new_fit = pd.DataFrame({'x': fit_func(df['x_val'].values, df['Y1'].values)[0],
                                'y': fit_func(df['x_val'].values, df['Y1'].values)[1]})
        source_f.data = new_fit


y_select = Select(options=['Y1', 'Y2'],value='Y1',title='y-axis data')
y_select.on_change('value', update_plot)

# Create layout and add to current document
layout = row(widgetbox(y_select), plot)

# Add layout to current document
curdoc().add_root(layout)

这一次,如果我更新图形,我看不到拟合曲线 我的问题。 另一个问题是:只有当我们更新绘图时,是否有任何优雅的方式来拟合曲线?

谢谢!

【问题讨论】:

  • 在update_plot,你应该有new_fit = {'x': answ[0], 'y': answ[1]}。我不确定我是否理解另一个问题。您的意思是最初不适合(dfit = ... 行)?
  • 太棒了!你解决了我的问题。根据您的回答,我认为我什至不需要定义 dfit DataFrame。我只需要使用 fit 函数定义 source_f 。我的意思是:xf, yf = fit_func(df['x_val'].values, df['Y1'].values) 然后source_f = ColumnDataSource(data={'x':xf, 'y':yf}) 也可以。

标签: python pandas plot bokeh curve-fitting


【解决方案1】:

考虑到Alex的评论,我把我的问题的解决方案放在这里:

import pandas as pd
import numpy as np
from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource, Select
from bokeh.plotting import figure
from scipy.optimize import curve_fit

def fit_func(xdata, ydata):

    def func(x,a,c):
        return a*(x**2)+a*x+c

    y = func(xdata, 1, 1)
    popt, pcov = curve_fit(func, xdata, ydata)
    new_x = np.arange(0,10,2)
    new_y = func(new_x,*popt)

    return (new_x, new_y)

dc = {'x_val':[0,1,2,3,4], 'Y1':[3.01, 5.10, 6.99, 8.02, 10.81],
     'Y2':[0.99, 3.05, 7.29, 13.41, 20.31]}
df = pd.DataFrame(dc)

source = ColumnDataSource(data={'x': df['x_val'], 'y': df['Y1'], 'y2':df['Y2']})

xf, yf = fit_func(df['x_val'].values, df['Y1'].values)
source_f = ColumnDataSource(data={'x': xf, 'y': yf})

plot = figure(x_axis_label = 'x', y_axis_label = 'f(x)')
plot.circle('x', 'y', source=source)
plot.line('x', 'y', source=source_f)

def update_plot(attr, old, new):
        y = y_select.value
        new_data = {'x': df['x_val'], 'y': df[y]}
        source.data = new_data

        answ = fit_func(df['x_val'].values, df[y].values)
        new_fit = {'x':answ[0], 'y':answ[1]}
        source_f.data = new_fit

y_select = Select(options=['Y1', 'Y2'],value='Y1',title='y-axis data')
y_select.on_change('value', update_plot)

layout = row(widgetbox(y_select), plot)    
curdoc().add_root(layout)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多