美文网首页
三、数据源的定义与转化

三、数据源的定义与转化

作者: Bllose | 来源:发表于2021-03-13 12:22 被阅读0次

同样的, 先来一波标准导入

from bokeh.io import output_notebook, show
from bokeh.plotting import figure
output_notebook()  

总览

在前面的教程中, 我们已经见识了Bokeh可以与Python lists, NumPy arrays, Pandas series等等, 完美协作。
在底层(At lower levels), 这些数据(Python lists, NumPy arrays ...)的输入实际上被Bokeh的ColumnDataSource统一接收了,
并屏蔽了针对这些不同输入的不同处理过程。
虽然Bokeh常常会帮我们创建这些ColumnDataSource。 但是有些时候,我们直接创建它也是很有用的。

通过Python字典创建

导入:
from bokeh.models import ColumnDataSource

注意: ColumnDataSource内定义的栏位长度必须保持一致

source = ColumnDataSource(data={
    'x' : [1, 2, 3, 4, 5],
    'y' : [3, 7, 8, 5, 1],
})

在之前的教程样例中,我们定义的标记方法(glyph)比如p.circle,我们都是将坐标数据直接写入对应的参数中。
其实我们也可以将我们定义的source属性赋值给source参数, 并且用对应的名字写入参数中。
比如:

p = figure(plot_width=400, plot_height=400)
p.circle('x', 'y', size=20, source=source)
show(p)
bokeh_plot (13).png

通过Pandas的DataFrames创建

通过Pandas data frames直接创建ColumnDataSource也很简单, 就是直接将data frame对象传入ColumnDataSource即可。

from bokeh.sampledata.iris import flowers as df

source = ColumnDataSource(df)

现在, 我们可以将这个ColumnDataSource和队列对应的名字传入标记方法(glyph)

p = figure(plot_width=400, plot_height=400)
p.circle('petal_length', 'petal_width', source=source)
show(p)   
bokeh_plot (14).png

自动转化

事实上, ColumnDataSource 对象在传入dicts, Pandas DataFrame或者GroupBy后能自动创建.
比如:

from bokeh.sampledata.iris import flowers as df

p = figure(plot_width=400, plot_height=400)
p.circle('petal_length', 'petal_width', source=df)
show(p)

转化为角度

from math import pi
import pandas as pd
from bokeh.palettes import Category20c
from bokeh.transform import cumsum

x = { 'United States': 157, 'United Kingdom': 93, 'Japan': 89, 'China': 63,
      'Germany': 44, 'India': 42, 'Italy': 40, 'Australia': 35, 'Brazil': 32,
      'France': 31, 'Taiwan': 31, 'Spain': 29 }

data = pd.Series(x).reset_index(name='value').rename(columns={'index':'country'})
data['color'] = Category20c[len(x)]

# represent each value as an angle = value / total * 2pi
data['angle'] = data['value']/data['value'].sum() * 2*pi

p = figure(plot_height=350, title="Pie Chart", toolbar_location=None,
           tools="hover", tooltips="@country: @value")

p.wedge(x=0, y=1, radius=0.4, 
        
        # use cumsum to cumulatively sum the values for start and end angles
        start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
        line_color="white", fill_color='color', legend_field='country', source=data)

p.axis.axis_label=None
p.axis.visible=False
p.grid.grid_line_color = None

show(p)
transform2angle.PNG

颜色依据x轴递进而改变

from bokeh.transform import linear_cmap

N = 4000
data = dict(x=np.random.random(size=N) * 100,
            y=np.random.random(size=N) * 100,
            r=np.random.random(size=N) * 1.5)

p = figure()

p.circle('x', 'y', radius='r', source=data, fill_alpha=0.6,
        
         # color map based on the x-coordinate
         color=linear_cmap('x', 'Viridis256', 0, 100))

show(p) 
bokeh_plot (15).png

通过log_cmap实现自定义颜色渐变

from bokeh.transform import log_cmap
p = figure()

p.circle(
    'x', 'y', 
    radius='r', 
    source=data, 
    fill_alpha=0.6,
        
    # 基于 x 坐标轴的色表
    color=log_cmap(
        'x', 'Viridis256', 
        low=5, 
        high=90,
        low_color="blue",
        high_color="red"
    )
)

show(p)
bokeh_plot (16).png

下一章:四、添加注解

相关文章

网友评论

      本文标题:三、数据源的定义与转化

      本文链接:https://www.haomeiwen.com/subject/lzlxcltx.html