带有图例的分类区域以及Python Bokeh中的havortool

时间:2017-09-26 08:48:05

标签: python charts categories bokeh area

有谁知道如何为python散景区域图表添加图例外的情节?我试图绘制分类区域图,看起来非常酷,适用于以下示例:

http://bokeh.pydata.org/en/latest/docs/gallery/brewer.html Bokeh patches plot with dates as x-axis shifts the ticks one to the right  how to show legend items of patches in bokeh

到目前为止,我想在情节之外添加传奇,我看到其他答案,但在这种情况下仍然不起作用。我试着在循环中

legend = Legend(items=[LegendItem(label=dict(field="area"), renderers=[r])], location=(0, -30)) 
p.add_layout(legend, 'right') 

给了我:RuntimeError:Plot Figure(id = ...)配置了多个图例渲染器

我还想添加HavorTool和ColumnDataSource。任何帮助将不胜感激。这是我试图在剧情和HavorTool之外添加传奇。

import numpy as np
from bokeh.palettes import brewer
import pandas as pd
from bokeh.plotting import figure, show, output_notebook, output_file
from bokeh.models import HoverTool
from bokeh.models import LegendItem, Legend

output_notebook()
N = 20
categories = ['y' + str(x) for x in range(10)]
data = {}
data['x'] = np.arange(1,N+1)
for cat in categories:
    data[cat] = np.random.randint(10, 100, size=N)

data11= pd.DataFrame(data)

data11 = data11.set_index(['x'])

def stacked(df, categories):
    areas = dict()
    last = np.zeros(len(df[categories[0]]))
    for cat in categories:
        next = last + df[cat]
        areas[cat] = np.hstack((last[::-1], next))
        last = next
    return areas

areas = stacked(data11, categories)

colors = brewer["Spectral"][len(areas)]

x2 = np.hstack((data['x'][::-1], data['x']))

timesteps = [str(x.date()) for x in pd.date_range('1950-01-01', '1951-08-01', freq='MS')]
p = figure(x_range=timesteps, y_range=(0, 800), toolbar_location="above")

p.grid.minor_grid_line_color = '#eeeeee'
for i, area in enumerate(areas):
    r = p.patch(x2, areas[area], color=colors[i], legend=area, alpha=0.8, line_color=None)
    ''' Add legend below that would match for each area? '''
    #legend = Legend(items=[LegendItem(label=dict(field="area"), renderers=[r])], location=(0, -30)) 
    #p.add_layout(legend, 'right') 

hover = HoverTool(tooltips=[('category', '@categories'), 
                             ('(y0,y1,y2,y3,y4,y5,y6,y7,y8,y9)','(@areas[y0], @areas[y1], @areas[y3], @areas[y4], @areas[y5], @areas[y6], @areas[y7], @areas[y8],@areas[y9])')]) 
p.add_tools(hover) #Add HavorTool not work? 

p.xaxis.major_label_orientation = np.pi/4

show(p) 

enter image description here

0 个答案:

没有答案