【问题标题】:Interactive Plot of Data frame: Drop Down menu to select columns to Display (Bokeh)数据框的交互式绘图:下拉菜单选择要显示的列(散景)
【发布时间】:2018-07-18 01:21:55
【问题描述】:

我是第一次尝试使用 Bokeh 库,但我发现文档不是那么简单。

我有一个数据框 df:

A   B   C
1   4   6
2   3   5
3   2   4
4   1   3

我想创建一个带有集成小部件的 Boken 直方图,以便用户选择要显示的列(A B 或 C)。

我写了以下内容:

import pandas as pd

d= {'A': [1, 2,3,4], 'cB': [4,3,2,1], 'C' : [6,5,4,3]}

df = pd.DataFrame(data=d)

names = ["A","B", "C"]

from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models.widgets import MultiSelect
from bokeh.io import curdoc

# drop table
curdoc().clear() 
# create drop down  #define witget

output_file("multi_select.html")
Field =  MultiSelect(title="Features:", value=["A"],
                           options=names)
show(widgetbox(Field))



from bokeh.charts import Histogram, output_file, show
from bokeh.layouts import row, layout
from bokeh.models.sources import ColumnDataSource
curdoc().clear()
source = ColumnDataSource(df)

hist = Histogram(df, values="A", title="A", plot_width=400) #not sure why I cannot use source instead of df

output_file('hist.html')
show(hist)

所以现在我需要将绘图与小部件连接起来。

我尝试了以下方法,但似乎不起作用。

hist = Histogram(df, values={'Field'}, title={'Field'}, plot_width=400)

欢迎任何其他不使用散景库的解决方案,我使用 Spyder 编辑器运行代码并使用 IE 来可视化结果。

【问题讨论】:

  • 您要一次选择单列还是多列。 @A.Papa
  • 最好是多列一次比较分布,但我不确定是否可能。

标签: python histogram bokeh interactive


【解决方案1】:

所以我仍然没有设法将散景与回调函数和数据框一起使用。然而,当我使用 Jupiter 时,我发现了一个非常简单的替代方案,

import matplotlib.pyplot as pl
hue = ["A", "B"]
@interact (col= ["A", "B", "C"])
def plot(col):
    pl.figure()
    pl.hist( df[col]) 
    pl.show()

plot(col)

【讨论】:

    【解决方案2】:

    使用此代码,您将能够与列进行交互。我正在使用最新版本的 Pandas、Numpy 和 Bokeh。在新的更新中,Bokeh.charts 已被弃用。

    import pandas as pd
    import numpy as np
    
    #Pandas version 0.22.0
    #Bokeh version 0.12.10
    #Numpy version 1.12.1
    
    from bokeh.io import output_file, show,curdoc
    from bokeh.models import Quad
    from bokeh.layouts import row, layout,widgetbox
    from bokeh.models.widgets import Select,MultiSelect
    from bokeh.plotting import ColumnDataSource,Figure,reset_output,gridplot
    
    d= {'A': [1,1,1,2,2,3,4,4,4,4,4], 'B': [1,2,2,2,3,3,4,5,6,6,6], 'C' : [2,2,2,2,2,3,4,5,6,6,6]}
    df = pd.DataFrame(data=d)
    names = ["A","B", "C"]
    
    #Since bokeh.charts are deprecated so using the new method using numpy histogram
    hist,edge = np.histogram(df['A'],bins=4)
    #This is the method you need to pass the histogram objects to source data here it takes edge values for each bin start and end and hist gives count.
    source = ColumnDataSource(data={'hist': hist, 'edges_rt': edge[1:], 'edges_lt':edge[:-1]})
    
    plot = Figure(plot_height = 300,plot_width = 400)
    #The quad is used to display the histogram using bokeh.
    plot.quad(top='hist', bottom=0, left='edges_lt', right='edges_rt',fill_color="#036564", 
              line_color="#033649",source = source)
    
    #When you change the selection it will this function and changes the source data so that values are updated.
    def callback_menu(attr, old, new):
    
        hist,edge = np.histogram(df[menu.value],bins=4)
        source.data={'hist': hist,'edges_rt': edge[1:], 'edges_lt': edge[:-1]}
    
    #These are interacting tools in the final graph
    menu = MultiSelect(options=names,value= ['A','B'], title='Sensor Data')
    menu.on_change('value', callback_menu)
    layout = gridplot([[widgetbox(menu),plot]])
    curdoc().add_root(layout)
    

    保存文件后,在同一目录中的 Anaconda 提示符下使用以下命令启动散景服务器,以便与图形进行交互。

    bokeh serve --show Python_Program_Name.py
    

    运行图表并选择后,如下所示

    【讨论】:

    • 谢谢Sandeep,这是否意味着我与散景服务器共享数据?为什么我需要连接散景来与图形交互?
    • @A.Papa Bokeh 服务器是本地的,它不会将您的数据发送到在线。这将有助于根据下拉列表中的选择更改值。如果没有散景服务器,您将无法更改图表中的值,或者您可以在笔记本本身中使用 ipywidgets,但不能在浏览器的新选项卡中使用。有效果吗?
    • 嗨,Sandeep,正在工作。但是,直方图彼此堆叠。当您添加更多列以用不同颜色呈现每列的直方图时,是否有可能?此外,您能否评论您的代码以了解您的解决方案背后的直觉?总的来说,干得好,我有一段时间正在为此苦苦挣扎!
    • @A.Papa 我认为没有选项可以在单个散景直方图中显示具有不同颜色代码的不同列值。但我对此不太确定。如果您想检查多个列的值,为什么不在同一个图表中为每一列分别创建一个直方图,这是可能的。
    • 感谢您的意见!真的很有帮助!
    猜你喜欢
    • 2022-08-20
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    相关资源
    最近更新 更多