如何在Bokeh中设置默认样式? [英] How to set the default style in Bokeh?
问题描述
我正在写一个报告,其所有图都用Matplotlib
渲染.我已经调整了Matplotlib
的默认值,以确保所有绘图都具有相同的样式.
I'm writing a report whose plots are all rendered with Matplotlib
. I've adjusted Matplotlib
's default to ensure that all plots have the same style.
但是,我需要使用Bokeh
,因为它为Datashader
的渲染图例提供了支持-这是一个由Bokeh
的人们开发的库.
However, I need to use Bokeh
since it provides support for rendering legends for Datashader
- a library being developed by the folks at Bokeh
.
我的问题是默认的Bokeh
样式与我的自定义样式有很大不同.除了可以更改样式表中的每个属性之外,还可以像使用Matplotlib
使用plt.use.style(['ggplot'])
一样从样式表中读取Bokeh
吗?
My issue is that the default Bokeh
style is very different from my custom style. Rather than changing every single attribute in my Bokeh
plot would it be possible to have Bokeh
read from a style sheet in a similar way as Matplotlib
does with plt.use.style(['ggplot'])
?
推荐答案
从Bokeh 0.12.4
开始,围绕Bokeh的主题仍然存在一些未解决的问题(要开发的功能以及一些错误以及更多的文档支持).当前支持的是使用Theme
对象的基于类型的主题,该对象可以在当前文档上设置.
As of Bokeh 0.12.4
there are still open issues (features to develop as well as a few bugs, and more documentation support) around theming in Bokeh. What is currently supported is type-based theming using a Theme
object that can be set on the current document.
Theme
对象采用通用形式的JSON块:
The Theme
object takes a JSON block, of the general form:
{
'attrs: {
'SomeTypeName': { 'foo_property': default_foo },
'OtherTypeName': { 'bar_property': default_bar }
}
}
或者举一个具体的例子:
Or for a concrete example:
from bokeh.io import curdoc
from bokeh.themes import Theme
curdoc().theme = Theme(json={'attrs': {
# apply defaults to Figure properties
'Figure': {
'toolbar_location': None,
'outline_line_color': None,
'min_border_right': 10,
},
# apply defaults to Axis properties
'Axis': {
'major_tick_in': None,
'minor_tick_out': None,
'minor_tick_in': None,
'axis_line_color': '#CAC6B6',
'major_tick_line_color': '#CAC6B6',
},
# apply defaults to Legend properties
'Legend': {
'background_fill_alpha': 0.8,
}
}})
也可以使用标准Python JSON工具从文件中读取此JSON.
This JSON could also be read from a file using standard Python JSON tools.
如果这恰好也是在(目录样式)Bokeh服务器应用程序的上下文中,则还可以将主题作为theme.yaml
文件提供在与main.py
相同的目录中.参见例如 Gapminder示例.
If this also happens to be in the context of a (directory style) Bokeh server application, you can also provide the theme as a theme.yaml
file in the same directory as your main.py
. See, e.g., the Gapminder example.
这篇关于如何在Bokeh中设置默认样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!