Python 无法使用Bokeh和Panda读取csv并进行打印

Python 无法使用Bokeh和Panda读取csv并进行打印,python,bokeh,Python,Bokeh,我试图从一个简单的CSV文件中绘制一个线条图,该文件有两列,使用Bokeh进行数据可视化,使用Panda读取CSV并处理数据。然而,我似乎无法将使用pandas导入的数据传递到Bokeh来绘制我的折线图 这在我的计算机上本地运行。我尝试并调试了代码的每一部分,唯一的问题似乎是在我将数据从pandas传递到bokeh时出现的 我已尝试打印从csv中选择的列,以检查是否也选择了整个列 #Requirements for App from bokeh.plotting import figure, o

我试图从一个简单的CSV文件中绘制一个线条图,该文件有两列,使用Bokeh进行数据可视化,使用Panda读取CSV并处理数据。然而,我似乎无法将使用pandas导入的数据传递到Bokeh来绘制我的折线图

这在我的计算机上本地运行。我尝试并调试了代码的每一部分,唯一的问题似乎是在我将数据从pandas传递到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)

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

这应该行得通。Pandas不知道它正在处理日期,因此您必须使用pd.to_datetime()指定它


这应该行得通。Pandas不知道它正在处理日期,因此您必须使用pd.to_datetime()指定它


非常感谢@Jasper!这帮助我得到了我想要的图表。只是好奇,为什么要将导入的csv转换为ColumnDataSource。这有必要吗?如果有,它有什么用途?不需要使用ColumnDataSource在这种情况下,它用于对Bokeh进行更高级的可视化(悬停显示数据、流数据等)。我看到你已经导入了它,所以我想展示一下你应该如何使用它会很好。我明白了!非常感谢贾斯珀:)@Jasper非常感谢你。这是旧的,但它在2021年救了我:)@Grantx很高兴听到:)非常感谢@Jasper!这帮助我得到了我想要的图表。只是好奇,为什么要将导入的csv转换为ColumnDataSource。这有必要吗?如果有,它有什么用途?不需要使用ColumnDataSource在这种情况下,它用于对Bokeh进行更高级的可视化(悬停显示数据、流数据等)。我看到你已经导入了它,所以我想展示一下你应该如何使用它会很好。我明白了!非常感谢贾斯珀:)@Jasper非常感谢你。这已经很老了,但它在2021年救了我:)@Grantx很高兴听到:)
#!/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)