散景服务器 - 从日期时间轴到分类选择

bokeh server- alternate from datetime axis to categorical on Selection

我有两个数据帧作为两个不同的 ColumnDataSources 用于更新绘图。一个长得像

ENTRYDATE | Transactions
2017-01-01  290
2017-01-03  20
..
2018-02-04  118

另一个数据框的第一列是第一个数据框的浮点数,由年份组成,后面是那一年的周数,用冒号分隔

ENTRYDATE | Transactions
2017.1      310
2018.1      118

我有一个 Select 小部件,它根据用户是想查看每日数据还是每周数据来更新线图。每日情节呈现良好。 However, when weekly is selected, the plot treats the weeks as number data there are large gaps from one data point to the next (i.e. 2017.52 to 2018.1)

如何编辑我的代码,以便在用户进行选择时分类显示每周数据?有没有更好的方法来表示跨多个日历年的周数?

这是我的代码的相关部分:

dfdate=(df2.groupby([df2['ENTRYDATE'].dt.date]).size().reset_index(name='Transactions'))
#WEEK
df['YearWk']=df['ENTRYDATE'].dt.strftime('%Y.%W') #add year column
dfweek=(df.groupby([df['YearWk']]).size().reset_index(name='Transactions'))
dfweek.columns.values[0]='ENTRYDATE' #rename column to ENTRYDATE 
dfweek.sort_values('ENTRYDATE',ascending=True) #sort by Year.WeekNumber

sourceDay=ColumnDataSource(data=dfdate)
sourceWeek=ColumnDataSource(data=dfweek)

p=figure(plot_width=800,plot_height=500, y_axis_label="Transaction Count",
x_axis_label="Date",background_fill_color='beige')
p.line(x="ENTRYDATE",y="Transactions",color='blue', source=sourceDay)
p.xaxis.formatter=DatetimeTickFormatter()

#update function
def update_plot(attr, old, new):
    if new=='Daily': 
        sourceDay.data={"ENTRYDATE":dfdate["ENTRYDATE"],"Transactions":dfdate["Transactions"]}
    elif new=='Weekly':
        sourceDay.data=sourceWeek.data
        p.x_range=str(dfweek["ENTRYDATE"].tolist())

#selecttool
select=Select(title='Choose Your Time Interval:', options=['Daily','Weekly'], value='daily')
select.on_change('value',update_plot)
layout=row(select, p)
curdoc().add_root(layout)

我尝试通过 p.x_range=dfweek["ENTRYDATE"].tolist()p.x_range.set=dfweek["ENTRYDATE"].tolist() 设置 x_range 但无济于事。

通过以下方式将周和年转换为日期时间 df = pd.to_datetime(df.YearWk.add('-0'), format='%Y-%W-%w') 根据