Python Bokeh vbar_堆栈条在过滤数据源后不会重新排序
我有一个vbar_堆栈图,我正在使用CheckboxGroup选项更新它。当我更改所选帐户时,数据确实会更改,但图表不会重新排序。我怎样才能使帐户按降序重新排序 现在,我将此代码用于checkboxgrouponchange命令。自定义_make_dataset函数会对过滤后的数据集进行重新排序Python Bokeh vbar_堆栈条在过滤数据源后不会重新排序,python,widget,bokeh,Python,Widget,Bokeh,我有一个vbar_堆栈图,我正在使用CheckboxGroup选项更新它。当我更改所选帐户时,数据确实会更改,但图表不会重新排序。我怎样才能使帐户按降序重新排序 现在,我将此代码用于checkboxgrouponchange命令。自定义_make_dataset函数会对过滤后的数据集进行重新排序 def custom_make_dataset(df, accounts): remove_accounts = [i for i in df["acc"].unique() if i not
def custom_make_dataset(df, accounts):
remove_accounts = [i for i in df["acc"].unique() if i not in accounts]
new_df = df \
.groupby(["acc","product"])["cost"] \
.sum() \
.reset_index() \
.pivot(columns="acc", index="product", values="cost") \
for account in remove_accounts:
new_df[account] = 0
new_df["Total"] = new_df.sum(axis=1)
new_df.sort_values(by="Total", ascending=False, inplace=True)
del new_df["Total"]
return ColumnDataSource(new_df)
def update_checkbox(attr, old, new):
accounts_to_plot = [account_selection.labels[i] for i in account_selection.active]
new_src = custom_make_dataset(df,accounts_to_plot)
source.data.update(new_src.data)
account_selection = CheckboxGroup(name="Customer Accounts", labels=initial_accounts, active = [i for i,j in enumerate(initial_accounts)])
account_selection.on_change('active', update_checkbox)
也许bokeh正在使用数据帧索引来确定值放置在哪个x位置 在返回新ColumnDataSource之前,请尝试重置新的_df索引。
new_df.reset_indexdrop=True,inplace=True我解决了这个问题。当我更新数据时,我没有更新x轴本身。可以在onchange命令中添加类似的内容来更新绘图x轴
p、 x_range.factors=新的_src.data['product']不起作用。我还使用ColumnDataSourcenew_df.data检查了ColumnDataSource,并且在数据存储中对值进行了正确排序。