【问题标题】:Bokeh how to make a trend line graph with error based on a pandas dataframe?Bokeh 如何根据 pandas 数据框制作带有错误的趋势线图?
【发布时间】:2019-01-17 15:59:40
【问题描述】:

我正在尝试制作与此类似的图表:

到目前为止,我的尝试是:

from bokeh.io import  show
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
import pandas as pd

from datetime import datetime

def date_(day):
    return datetime(2018, 1, day)


df = pd.DataFrame(
{
"date": [date_(1),date_(2),date_(3),date_(4),date_(5),date_(6),date_(7),date_(8),date_(9),date_(10)],
"mean": [10,8,9, 11,12,6, 8,3,8,7],
"std": [2,1,3,2,1,4, 2,3,1,4]
})
df['mean_p_std'] = df['mean'] + df['std']
df['mean_m_std'] = df['mean'] - df['std']

source = ColumnDataSource(data=df)

plot = figure(x_axis_type="datetime", plot_width=800, plot_height=350)
plot.line('date', 'mean', source=source ,line_color='black', line_width=4)

plot.patch('date','mean_p_std',alpha=0.5, line_width=2, source=source)
plot.patch('date','mean_m_std',alpha=0.5, line_width=2, source=source)

show(plot)

我尝试计算上限和下限,但我没有做对

【问题讨论】:

  • 看来您必须计算数据集的上限和下限。
  • @Scratch'N'Purr 我累了,但没用。我更新了问题。

标签: python pandas data-visualization bokeh


【解决方案1】:

你必须在你的补丁中构建你的xy 数组。它需要做一些附加。

>>> plot = figure(x_axis_type="datetime", plot_width=800, plot_height=350)
>>> plot.line('date', 'mean', source=source ,line_color='black', line_width=4)
>>> x = df['date'].append(df['date'].sort_index(ascending=False))
>>> y = df['mean_p_std'].append(df['mean_m_std'].sort_index(ascending=False))

然后你绘制你的补丁:

>>> plot.patch(x=x, y=y, alpha=0.5, line_width=2)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-08-28
  • 2021-04-26
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
相关资源
最近更新 更多