您如何在bokeh中创建多行图标题?...与https://github.com/bokeh/bokeh/issues/994相同的问题
是否已解决?
import bokeh.plotting as plt
plt.output_file("test.html")
plt.text(x=[1,2,3], y = [0,0,0], text=['hello\nworld!', 'hello\nworld!', 'hello\nworld!'], angle = 0)
plt.show()
Run Code Online (Sandbox Code Playgroud)
另外,标题文本字符串可以接受格式文本吗?
在 Bokeh 的最新版本中,标签和文本字形可以接受文本中的换行符,这些将按预期呈现。对于多行标题,您必须为Title所需的每一行添加明确的注释。这是一个完整的例子:
from bokeh.io import output_file, show
from bokeh.models import Title
from bokeh.plotting import figure
output_file("test.html")
p = figure(x_range=(0, 5))
p.text(x=[1,2,3], y = [0,0,0], text=['hello\nworld!', 'hello\nworld!', 'hello\nworld!'], angle = 0)
p.add_layout(Title(text="Sub-Title", text_font_style="italic"), 'above')
p.add_layout(Title(text="Title", text_font_size="16pt"), 'above')
show(p)
Run Code Online (Sandbox Code Playgroud)
其中产生:
请注意,您仅限于 Bokeh 公开的标准“文本属性”,因为底层 HTML Canvas 不接受富文本。如果您需要类似的东西,可以使用自定义扩展
fuk*_*uri -1
您可以使用以下命令向绘图添加一个简单的标题:
from bokeh.plotting import figure, show, output_file
output_file("test.html")
p = figure(title="Your title")
p.text(x=[1,2,3], y = [0,0,0], text=['hello\nworld!', 'hello\nworld!', 'hello\nworld!'], angle = 0)
show(p)
Run Code Online (Sandbox Code Playgroud)
附录
这是一个绘制 pandas 数据框的工作示例,供您复制/粘贴到 jupyter 笔记本中。它既不优雅也不Pythonic。我很久以前就从各种 SO 帖子中得到了它。抱歉,我不记得是哪些了,所以我无法引用它们。
代码
# coding: utf-8
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
import pandas as pd
import numpy as np
# Create some data
np_arr = np.array([[1,1,1], [2,2,2], [3,3,3], [4,4,4]])
pd_df = pd.DataFrame(data=np_arr)
pd_df
# Convert for multi-line plotting
data = [row[1].as_matrix() for row in pd_df.iterrows()]
num_lines = len(pd_df)
cols = [pd_df.columns.values] * num_lines
data
# Init bokeh output for jupyter notebook - Adjust this to your needs
output_notebook()
# Plot
p = figure(plot_width=600, plot_height=300)
p.multi_line(xs=cols, ys=data)
show(p)
Run Code Online (Sandbox Code Playgroud)
阴谋