如何使用堆积条形图在情节区域之外制作图例 [英] How can I make legend outside plot area with stacked bar

查看:84
本文介绍了如何使用堆积条形图在情节区域之外制作图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Bokeh库有问题.我需要在情节区域之外创建一个图例框.它适用于圆和面积绘图.我找不到堆叠条形的解决方案,图例下方的解决方案塑造了图形的一部分,因此不可接受(请参见代码下的链接).我想在框中输入图例.

I have a problem with Bokeh library. I need make a legend box outside plot area. It works for circle and area plotting. I can't find the solution for the stacked bar.With the solution below the legend shapes one part of the graph, it isn't acceptable (see link under the code).I would like a legend in the box.

关于, SR

import numpy as np
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource, 
NumeralTickFormatter,LinearAxis, Range1d, Legend
from bokeh.plotting import show, output_notebook, figure 
from bokeh.core.properties import value
from math import pi
output_notebook()
colors=['#47A440','#9FCDC8']
source = ColumnDataSource(F)
mois=F.mois.tolist()

def insert(colonne, position1, position2,valeur1, valeur2):
    li=[]
    li=colonne.tolist()
    li.insert(position1,valeur1)
    li.insert(position2, valeur2)
    return li
Data_mois_area= insert(F.mois,0,12,'Janvier','Décembre')
Data_conso_area= insert(F.Pui_Conso,0,13,0,0)
Data_pv_area= insert(F.Pui_PV,0,13,0,0)
p = figure(x_range=mois, plot_height=500, 
plot_width=800,toolbar_location=None,tools="hover")
xa=range(len(F.mois))
r0=p.patch(x=Data_mois_area, y=Data_conso_area, color="#003BFB")
r1=p.patch(x=Data_mois_area, y=Data_pv_area, color="#FBB000")
p.vbar_stack(['Pui_Autocon','Pui_Réinj'], x='mois', width=0.5, 
color=colors,line_width=1, line_color='black',
         legend=['Autoconsommation','Réinjection'], source=source)
r3=p.circle(x='mois', y='Taux_autocon', size=10, color="red", 
y_range_name="foo", source=source )
r4=p.circle(x='mois', y='Taux_autopro', size=10, color="purple", 
y_range_name="foo", source=source)
p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.axis.minor_tick_line_color = None
p.outline_line_color = None
p.title.text = "Besoins et Consommation"
p.title.align = "center"
p.title.text_color = "black"
p.title.text_font_size = "15px"
p.xaxis.major_label_orientation = pi/4
p.yaxis.major_label_orientation = "horizontal"
p.yaxis.axis_label = "Energie en kWh"
p.yaxis.axis_label_text_font_style = "italic"
p.extra_y_ranges['foo'] = Range1d(0, 1.02)
p.add_layout(LinearAxis(y_range_name="foo"), 'right')
p.legend.orientation = "horizontal"
legend = Legend(items=[
("Consommation",   [r0]),
("Production", [r1]),
("Taux autoconsommation", [r3]),
("Taux autoproduction ",[r4]),    
 ], location=(1, -30))
 p.add_layout(legend, 'right')}
 show(p) 

在此处输入图像描述

推荐答案

我解决此问题的方法如下(hbar堆栈示例,但解决方案相同)

The way I've solved this is as follows (example for hbar stack but the solution is identical)

# make a figure
p = figure(y_range=uniquesubjects, x_range=(0,600), plot_height=900, plot_width=900, title="Subjects In Trouble!",
        toolbar_location=None, tools="")

# add stacked horizontal bars
p.hbar_stack(headers[0:-1], y='subject_name', color=colors, height=1, source=source, legend=[value(x) for x in legendvals])

# add some graph options
p.xgrid.grid_line_color = None
p.axis.minor_tick_line_color = None
p.outline_line_color = None
p.legend.location = "top_right"
p.legend.orientation = "vertical"

new_legend = p.legend[0]
p.legend[0].plot = None
p.add_layout(new_legend, 'right')

show(p)

我像往常一样创建一个内部图例",然后制作一个副本,将其从原始位置删除,然后将其添加为外部布局.这是在以下三行中完成的:

I create an "internal legend", as normal and then I take a copy of it, remove it from the original location and add it as a layout outside. This is done in these three lines:

new_legend = p.legend[0]
p.legend[0].plot = None
p.add_layout(new_legend, 'right')

这篇关于如何使用堆积条形图在情节区域之外制作图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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