问题描述
我试图避免使用ColumnDataSource,而是将pandas dataframe列直接传递给Bokeh图.
I was trying to avoid using a ColumnDataSource and instead of that I was passing pandas dataframe columns directly to Bokeh plots.
尽管很快我必须实现一个HoverTool,它要求将数据存储在ColumnDataSource中.因此,我开始使用ColumnDataSource.
Soon though I had to implement a HoverTool which requires to have the data in a ColumnDataSource. So, I started using ColumnDataSource.
现在,我正在创建一个框注释,并且必须使用数据中某列的最大值来定义框的顶部边框.
Now, I was creating a box annotation and I had to use the maximum value of a certain column from my data to define the top border of the box.
我可以使用熊猫轻松做到这一点:
I can do that easily using pandas:
low_box = BoxAnnotation(
top=flowers['petal_width'][flowers['species']=='setosa'].max(),
fill_alpha=0.1, fill_color='red')
但是我不知道如何从ColumnDataSource中提取最大值.
But I can't figure out how to extract the maximum from a ColumnDataSource.
是否有一种方法可以从中提取最大值,或者我的方法首先是错误的?
Is there a way to extract a maximum value from it, or is my approach all wrong in the first place?
推荐答案
ColumnDataSource对象具有属性data
,该属性将首先返回用于创建该对象的python字典.
A ColumnDataSource object has an attribute data
which will return the python dictionary used to create the object in the first place.
from bokeh.plotting import ColumnDataSource
# define ColumnDataSource
source = ColumnDataSource(
data=dict(
x=[1, 2, 3, 4, 5],
y=[2, 5, 8, 2, 7],
desc=['A', 'b', 'C', 'd', 'E'],
)
)
# find max for variable 'x' from 'source'
print( max( source.data['x'] ))
这篇关于如何从Bokeh ColumnDatasource中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!