gpt4 book ai didi

python-3.x - "BokehUserWarning: ColumnDataSource' s 列的长度必须相同”

转载 作者:行者123 更新时间:2023-12-05 08:27:11 25 4
gpt4 key购买 nike

我正在尝试绘制可追溯到 1998 年的大量数据。

我的代码似乎工作正常,但运行时抛出错误消息“BokehUserWarning:ColumnDataSource 的列必须具有相同的长度”

这是我的代码:

import pandas as pd
from bokeh.io import show, output_file, gridplot
from bokeh.plotting import figure

#Create dataframe
df = pd.read_csv('/Users/macbook/Desktop/source.tab', names=[
'#','datesent','total','place'] delimiter='\t', header=None, encoding="ISO-8859-1")

#Format date
df['datesent'] = pd.to_datetime(df['datesent'], dayfirst=True)

#Datamunging
transactionssent = dict(pd.melt(df,value_vars=['datesent']).groupby('value').size())
transactionssent_dataframe = pd.DataFrame.from_dict(transactionssent, orient= 'index')
transactionssent_dataframe.columns = ['Number of sent transactions']
transactionssent_dataframe.index.rename('Date of sending', inplace= True)

#X- and Y-axis
x = pd.bdate_range('2017-1-1', '2200-1-1')
y = transactionssent_dataframe['Number of sent transactions']

#Bokeh object
ts = figure(x_axis_type="datetime")

#Show plot
ts.line(x, y)

output_file('/Users/macbook/Desktop/plot.html')

所有输出实际上都符合预期。错误是什么意思?我真的必须从数据框创建 ColumndDataSource 对象吗?我认为将 pandas 数据帧直接传递给 Bokeh 绘图函数是获得我想要的图形的好方法。是否有从 pandas dateframe 创建 Bokeh 图的最佳实践?

最佳答案

我假设验证错误来自于您的 xy 系列的长度不同。如果有意义的话,输出可能会切断较长阵列的悬垂部分。

您不必“必须”手动创建一个 ColumnDataSource(当您将数组传递给像 line 这样的字形方法时会在内部创建一个),但它有一些验证内容可以帮助防止这种情况.

您可以通过以下方式直接从数据框创建 ColumnDataSource:

source = ColumnDataSource(dataframe)
ts.line(x='x', y='y', source=source)

关于python-3.x - "BokehUserWarning: ColumnDataSource' s 列的长度必须相同”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44823050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com