如何从bokeh中的数据帧制作折线图?

时间:2019-01-29 03:51:32

标签: python graph line bokeh

我正在bokeh中读取.csv文件,该文件有两列:一列用于日期,一列用于与该日期相对应的值。我正在尝试使用x轴上的日期和y上的值制作线形图,但是它不起作用。有什么想法吗?

代码:

import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource
from datetime import datetime
from bokeh.palettes import Spectral3

output_file('output.html')

df = pd.read_csv('speedgraphak29.csv')
p = figure(x_axis_type="datetime")
p.line(x=df.dates, y=df.windspeed, line_width=2)


show(p)

它返回一个空图。我该怎么办?

2 个答案:

答案 0 :(得分:1)

由于您没有提供输入数据的示例,因此我必须进行补充。您可能忘记了指定date列应解释为bigreddot指出的datetime值。这是一个工作示例:

import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource
from datetime import datetime
from bokeh.palettes import Spectral3

output_file('output.html')

df = pd.DataFrame.from_dict({'dates': ["1-1-2019", "2-1-2019", "3-1-2019", "4-1-2019", "5-1-2019", "6-1-2019", "7-1-2019", "8-1-2019", "9-1-2019", "10-1-2019"], 'windspeed': [10, 15, 20,30 , 25, 5, 15, 30, 35, 25]})
df['dates'] = pd.to_datetime(df['dates'])
source = ColumnDataSource(df)
p = figure(x_axis_type="datetime")
p.line(x='dates', y='windspeed', line_width=2, source=source)

show(p)

答案 1 :(得分:0)

您可以使用它。假设您有一个名为sample_data.csv的CSV,其列为DateAmount。只是为了补充Jasper的东西。

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



output_file('output.html')

df = pd.read_csv('sample_data.csv', parse_dates=['Date'])

source = ColumnDataSource(df)

p = figure(x_axis_type="datetime")
p.line(x='Date', y='Amount', line_width=2, source=source)

show(p)

在这种情况下,请以该列作为日期格式读取CSV 。使用ColumnDataSource可让您使用高级功能,例如将鼠标悬停在图上以查看更多详细信息(如果需要)。 您也可以选择直接使用看起来像这样的列表。

p.line(x='my_list_of_dates', y='my_list_of_counts', line_width=2)

这意味着阅读每一列并从中列出。总而言之,使用ColumnDataSource将允许您直接按列名调用它。

相关问题