更改bokeh图中选项卡的样式

时间:2019-04-23 08:27:37

标签: python bokeh

我想知道是否可以更改散景图上产生的选项卡的属性。诸如增加文本字体,更改标签宽度等更改。以下是用于生成具有两个选项卡的绘图的简单代码。

from bokeh.models.widgets import Panel, Tabs
from bokeh.io import output_file, show
from bokeh.plotting import figure

output_file("slider.html")

p1 = figure(plot_width=300, plot_height=300)
p1.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)
tab1 = Panel(child=p1, title="circle")

p2 = figure(plot_width=300, plot_height=300)
p2.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=3, color="navy", alpha=0.5)
tab2 = Panel(child=p2, title="line")

tabs = Tabs(tabs=[ tab1, tab2 ])

show(tabs)

1 个答案:

答案 0 :(得分:2)

您可以覆盖默认的“散景”标签样式,如下所示(适用于Bokeh v1.1.0)。

  

请注意,Bokeh CSS库的实现可能会在   即将发布的版本。

下面找到的css样式适用于v1.1.0,但向后兼容v1.0.4

from bokeh.plotting import save, figure
from bokeh.models import Paragraph, Panel, Tabs, Column

template = """
{% block postamble %}
<style>
.bk-root .bk-tab {
    background-color: cyan;
    width: 200px;
    color: red;
    font-style: italic;
}

.bk-root .bk-tabs-header .bk-tab.bk-active{
background-color: yellow;
color: blue;
font-style: normal;
font-weight: bold;
}

.bk-root .bk-tabs-header .bk-tab:hover{
background-color: yellow
}

</style>
{% endblock %}
"""

p = Paragraph(text = "Another tab", width = 600)

plot = figure()
plot.line(x = [1, 2], y = [3, 4])
tabs = [Panel(title = 'Tab1', child = plot)]
tabs.append(Panel(title = 'Tab2', child = p))

save(Column(Tabs(tabs = tabs, width = 600)), template = template)

结果:

enter image description here

相关问题