无法使用Bokeh和Panda读取csv并将其绘制出来

时间:2019-01-03 11:27:24

标签: python bokeh

我正在尝试使用Bokeh从数据简单的CSV文件绘制带有两列的折线图,以实现数据可视化,并使用Panda读取CSV并处理数据。但是,我似乎无法将使用熊猫导入的数据传递给Bokeh来绘制我的线图。

这在我的计算机上本地运行。我已经尝试并调试了代码的每个部分,当我将数据从熊猫传递到bokeh时,似乎出现了唯一的问题。

我尝试打印从csv中选择的列,以检查是否也选中了整个列。

#Requirements for App
from bokeh.plotting import figure, output_file, show
import pandas as pd
from bokeh.models import ColumnDataSource

#Import data-->Weight measurements over a period of time [ STUB ]
weight = pd.read_csv("weight.csv")

#Define parameters
x=weight["Date"]
y=weight["Weight"]

#Take data  and present in a graph
output_file("test.html")
p = figure(plot_width=400, plot_height=400)
p.line(x,y,line_width=2)
show(p)

我希望得到一个折线图,该图可以每天绘制每个体重条目的图,但是我得到一个空白图。

1 个答案:

答案 0 :(得分:1)

这应该有效。熊猫不知道它正在处理日期,因此您必须使用pd.to_datetime()进行指定。

#!/usr/bin/python3
from bokeh.plotting import figure, output_file, show
import pandas as pd
from bokeh.models import DatetimeTickFormatter, ColumnDataSource

#Import data-->Weight measurements over a period of time [ STUB ]
weight = pd.read_csv("weight.csv")

#Define parameters
weight["Date"] = pd.to_datetime(weight['Date'])
weight["Weight"] = pd.to_numeric(weight['Weight'])

source = ColumnDataSource(weight)

#Take data  and present in a graph
output_file("test.html")
p = figure(plot_width=400, plot_height=400, x_axis_type='datetime')
p.line(x='Date',y='Weight',line_width=2, source=source)
p.xaxis.formatter=DatetimeTickFormatter(
    minutes=["%M"],
    hours=["%H:%M"],
    days=["%d/%m/%Y"],
    months=["%m/%Y"],
    years=["%Y"]
)
show(p)