选择散景图中散点图的数据框行 [英] Selecting dataframe rows for a scatter plot in bokeh

查看:101
本文介绍了选择散景图中散点图的数据框行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Bokeh中的下拉菜单从数据框中选择特定行的最佳方法是什么?我尝试使用该虹膜下的修改后的脚本,但服务器仅出现黑屏:

What is the best way to select specific rows from a dataframe using a dropdown meun in Bokeh? I've tried using the modified script below that charts iris but got only a blank screen the server:

import pandas as pd

from bokeh.models import ColumnDataSource, ColorBar, CategoricalColorMapper
from bokeh.plotting import figure, show
from bokeh.palettes import Spectral6
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.layouts import widgetbox

from bokeh.sampledata.iris import flowers as df
source = ColumnDataSource(df)

mapper = CategoricalColorMapper(
    factors=['setosa', 'virginica', 'versicolor'], 
    palette=['red', 'green', 'blue']) 

plot = figure(x_axis_label='petal_length', y_axis_label='sepal_length',plot_width=400,plot_height=400)
plot.circle('petal_length', 'sepal_length',size=4, source=source, 
            color={'field': 'species', 
                   'transform': mapper}) 

species=list (df['species'].unique())

menu = Select(options=species,value='setosa', title='Species')

# Add callback to widgets
def callback(attr, old,new):
    source_data=pd.DataFrame(source.data)
    new_data= source_data.loc[source_data['species']==menu.value]
    new_data_dict=dict (new_data)
    source.data=new_data_dict

menu.on_change('value', callback)

layout = column(menu, plot)
curdoc().add_root(layout)

第一部分是给定的示例,因此没有菜单的图表工作正常.我的问题是回调函数的设计,以从数据框中选择特定的行.

The first part is a given example so the the chart without the menu works fine. My issue is with the design of the callback function to select specific rows from the dataframe.

推荐答案

似乎您忘了为.loc指定行输入,我假设您希望种类列等于menu.value的所有行.试试这个代替您的new_data分配

It looks like you forgot to specify the row input for .loc which I assume you want all rows where the species column equals menu.value. Try this in place of your new_data assignment

new_data = source_data.loc[:,source_data['species']==menu.value]

或者,如果没有.loc,则可以使用布尔值遮罩

Alternatively without .loc you could use boolean masking

new_data = source_data[source_data['species'] == menu.value]

这篇关于选择散景图中散点图的数据框行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆