python - 使用 Bokeh 绘制时间序列图?

标签 python python-2.7 bokeh

我有这样一本字典:

dic = {'2017-08-11': {'Yes': 157, 'Not sure': 2, 'No': 1}, '2017-08-22': {'Yes': 142, 'Not sure': 12}, '2017-08-01': {'Yes': 112, 'Others': 10, 'Not sure': 4, 'No': 9}, '2017-08-17': {'Yes': 117, 'No': 12, 'Not sure': 11, 'Others': 2}, '2017-08-25': {'Yes': 61, 'Not sure': 9}, '2017-08-23': {'Yes': 268, 'Not sure': 20, 'No': 1}, '2017-07-10': {'Yes': 123, 'Not sure': 4, 'No': 1}, '2017-08-10': {'Yes': 343, 'Not sure': 20}, '2017-07-13': {'Yes': 116, 'Others': 1, 'Not sure': 14, 'No': 2}, '2017-07-14': {'Yes': 255, 'Not sure': 22, 'No': 6}, '2017-08-07': {'Yes': 73, 'Others': 3, 'Not sure': 4, 'No': 5}, '2017-08-04': {'Not sure': 11, 'Others': 8, 'Yes': 178, 'No': 10}, '2017-08-16': {'Not sure': 10, 'Yes': 219}, '2017-07-18': {'Yes': 1, 'No': 1}, '2017-08-15': {'Yes': 301, 'Others': 4, 'Not sure': 37, 'No': 31}, '2017-08-08': {'Yes': 38, 'No': 2, 'Others': 1}, '2017-08-09': {'Yes': 120, 'Not sure': 3}, '2017-08-28': {'Yes': 206, 'Others': 2, 'Not sure': 18, 'No': 24}, '2017-08-14': {'Yes': 46, 'No': 3, 'Not sure': 5, 'Others': 7}}

然后我使用字典使用 Bokeh 生成时间序列图:

from bokeh.charts import TimeSeries
from bokeh.io import output_file, show
from bokeh.io import output_notebook
from dateutil.parser import parse
output_notebook()
list_x = []
list_y = []
for i in dic:
    list_x.append(i)
    list_y.append(round(float(dic[i]['Yes'])/float(sum(dic[i].values()))*100, 2))
df_date = pd.DataFrame(columns=['Date', 'Precision'])
df_date['Date'] = list_x
df_date['Precision'] = list_y
df_date.sort_values('Date', ascending=True, inplace=True)
x = df_date['Date'].tolist()
y = df_date['Precision'].tolist()
data = dict(Precision = y, Date = [parse(s) for s in x])
p = TimeSeries(data, ylabel='Precision(%)')
output_file("timeseries.html")
show(p)

result没有在 x 轴上显示日期,如果有人能帮助我,我会很高兴,谢谢!

最佳答案

TimeSeries 是旧的 bokeh.charts API 的一部分,该 API 已被移除到一个单独的新 bkcharts 项目中。它目前没有维护,我强烈反对使用它。但是,您可以使用稳定的 bokeh.plotting API 轻松创建时间序列图。这是使用您的数据的最小示例(我也进行了更新以更好地使用 Pandas,并且没有显式循环)

import pandas as pd

from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show, output_file

dic = {'2017-08-11': {'Yes': 157, 'Not sure': 2, 'No': 1}, '2017-08-22': {'Yes': 142, 'Not sure': 12}, '2017-08-01': {'Yes': 112, 'Others': 10, 'Not sure': 4, 'No': 9}, '2017-08-17': {'Yes': 117, 'No': 12, 'Not sure': 11, 'Others': 2}, '2017-08-25': {'Yes': 61, 'Not sure': 9}, '2017-08-23': {'Yes': 268, 'Not sure': 20, 'No': 1}, '2017-07-10': {'Yes': 123, 'Not sure': 4, 'No': 1}, '2017-08-10': {'Yes': 343, 'Not sure': 20}, '2017-07-13': {'Yes': 116, 'Others': 1, 'Not sure': 14, 'No': 2}, '2017-07-14': {'Yes': 255, 'Not sure': 22, 'No': 6}, '2017-08-07': {'Yes': 73, 'Others': 3, 'Not sure': 4, 'No': 5}, '2017-08-04': {'Not sure': 11, 'Others': 8, 'Yes': 178, 'No': 10}, '2017-08-16': {'Not sure': 10, 'Yes': 219}, '2017-07-18': {'Yes': 1, 'No': 1}, '2017-08-15': {'Yes': 301, 'Others': 4, 'Not sure': 37, 'No': 31}, '2017-08-08': {'Yes': 38, 'No': 2, 'Others': 1}, '2017-08-09': {'Yes': 120, 'Not sure': 3}, '2017-08-28': {'Yes': 206, 'Others': 2, 'Not sure': 18, 'No': 24}, '2017-08-14': {'Yes': 46, 'No': 3, 'Not sure': 5, 'Others': 7}}

df = pd.DataFrame.from_dict(dic, orient="index")
df = df.fillna(0)
df.index = pd.to_datetime(df.index)
df.index.name = 'Date'
df.sort_index(inplace=True)

df['Total'] = df.Yes + df['Not sure'] + df.No + df.Others
df['Precision'] = round(df.Yes/df.Total, 2)

source = ColumnDataSource(df)

p = figure(x_axis_type="datetime", plot_width=800, plot_height=350)
p.line('Date', 'Precision', source=source)

output_file("ts.html")
show(p)

哪个产量

enter image description here

关于python - 使用 Bokeh 绘制时间序列图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45972782/

相关文章:

Python - 设置 .pop() 行为

javascript - 通过 Selenium 抓取动态内容?

python - 如果先添加处理程序,为什么 Python 日志记录只隐藏 stdout?

python - 将 Bokeh 图平移限制在定义的范围内

python - 运行任意Python代码的Bokeh悬停工具

javascript - 带有自定义图标的 Bokeh 自定义操作

Python - 如何根据 DF 和其中的数据制作 SQL "Create Table"语句?

Python:一台计算机上的 IDE,另一台计算机上的解释器

python - matplotlib 中是否有制作散点图矩阵的功能?

python - 在 tensorflow 中沿任何轴归一化意味着什么?