来自索引数据帧的交互式散点图

时间:2018-10-05 17:44:12

标签: python pandas data-visualization bokeh

这是我正在处理的分组DataFrame的一部分。我有几个国家和年份的几个变量。如何使用bokeh绘制散点图,其在x轴上显示gdp_share,在y轴上显示military_exp,以及从2010年到2017年(数据框覆盖的年份)的滑块? / p>

                  gdp_share military_exp    pop gdp gdp_pc  military_pc
year    Country                     
2010    China   2.0 138028416.0 1359755 13615620000 10226.87    101.509769
        France  2.0 54569820.0  63027   2559460000  39449.38    865.816555
        Germany 1.0 41488240.0  80895   3587004000  43928.90    512.865319
        Israel  6.0 15044490.0  7426    241692000   32665.37    2025.921088
2011    China   2.0 149022400.0 1367480 14957177000 11180.83    108.975927
        France  2.0 53235512.0  63344   2612677000  40078.52    840.419172
        Germany 1.0 40301428.0  80934   3718288000  45633.17    497.954234
        Israel  6.0 15582490.0  7569    254299000   33733.19    2058.725063
2012    China   2.0 161796896.0 1375199 16211650000 12058.70    117.653442
        France  2.0 52499752.0  63640   2617451000  39961.04    824.948963
        Germany 1.0 41262500.0  81066   3736582000  45771.68    508.998840
        Israel  6.0 15728390.0  7699    259884000   33840.45    2042.913365

这是我到目前为止一直在尝试的事情:

from bokeh.io import output_file, show, curdoc
from bokeh.layouts import widgetbox, column
from bokeh.models import ColumnDataSource, Slider
from bokeh.plotting import figure

N = data.unstack("year")
source = ColumnDataSource(data = data)

#Create plot and widgets
plot = figure()
plot.circle(x = data.unstack('Country')['gdp_pc'], y = data.unstack('Country')['military_pc'], source = source)

slider = Slider(start = 2010, end = 2017, value = N, step = 1, title = "Years")

#Add callback to widget
def callback(attr, old, new):
    N = slider.value
    source.data = {"x" : data.unstack('Country')['gdp_pc'], "y" : data.unstack('Country')['military_pc']}
slider.on_change("value", callback)

#Arrange plots and widgets in layouts
layout = column(slider, plot)

curdoc().add_root(layout)

我想念什么?这是我不断得到的错误:

RuntimeError: 
Supplying a user-defined data source AND iterable values to glyph methods is
not possibe. Either:

Pass all data directly as literals:

    p.circe(x=a_list, y=an_array, ...)

Or, put all data in a ColumnDataSource and pass column names:

    source = ColumnDataSource(data=dict(x=a_list, y=an_array))
    p.circe(x='x', y='y', source=source, ...)

0 个答案:

没有答案