- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试做一些我通常认为微不足道但在 Bokeh 中似乎非常困难的事情:将垂直颜色条添加到绘图中,然后将颜色条的标题(也就是颜色映射背后的变量)显示在一侧颜色条,但从水平方向顺时针旋转 90 度。
根据我对 Bokeh ColorBar() 界面的了解(查看文档并为此元素使用 python 解释器的 help() 函数),这是不可能的。无奈之下,我添加了自己的基于 Label() 的注释。这可行,但很笨拙,并且在 Bokeh 服务情况下部署时会显示奇怪的行为 - 图上数据窗口的宽度与标题颜色栏的标题字符串的长度成反比变化。
下面我包含了 Bokeh 服务器 mpg 示例的修改版本。为它的复杂性道歉,但我觉得这是使用 Bokeh 附带的基础设施/数据来说明问题的最佳方式。对于那些不熟悉 bokeh serve 的人,这个代码片段需要保存到一个名为 main.py 的文件中,该文件位于一个目录中——为了参数,假设是 CrossFilter2——并且在 CrossFilter2 的父目录中需要调用命令
bokeh serve --show CrossFilter2
import numpy as np
import pandas as pd
from bokeh.layouts import row, widgetbox
from bokeh.models import Select
from bokeh.models import HoverTool, ColorBar, LinearColorMapper, Label
from bokeh.palettes import Spectral5
from bokeh.plotting import curdoc, figure, ColumnDataSource
from bokeh.sampledata.autompg import autompg_clean as df
df = df.copy()
SIZES = list(range(6, 22, 3))
COLORS = Spectral5
# data cleanup
df.cyl = df.cyl.astype(str)
df.yr = df.yr.astype(str)
columns = sorted(df.columns)
discrete = [x for x in columns if df[x].dtype == object]
continuous = [x for x in columns if x not in discrete]
quantileable = [x for x in continuous if len(df[x].unique()) > 20]
def create_figure():
xs = df[x.value].tolist()
ys = df[y.value].tolist()
x_title = x.value.title()
y_title = y.value.title()
name = df['name'].tolist()
kw = dict()
if x.value in discrete:
kw['x_range'] = sorted(set(xs))
if y.value in discrete:
kw['y_range'] = sorted(set(ys))
kw['title'] = "%s vs %s" % (y_title, x_title)
p = figure(plot_height=600, plot_width=800,
tools='pan,box_zoom,wheel_zoom,lasso_select,reset,save',
toolbar_location='above', **kw)
p.xaxis.axis_label = x_title
p.yaxis.axis_label = y_title
if x.value in discrete:
p.xaxis.major_label_orientation = pd.np.pi / 4
if size.value != 'None':
groups = pd.qcut(df[size.value].values, len(SIZES))
sz = [SIZES[xx] for xx in groups.codes]
else:
sz = [9] * len(xs)
if color.value != 'None':
coloring = df[color.value].tolist()
cv_95 = np.percentile(np.asarray(coloring), 95)
mapper = LinearColorMapper(palette=Spectral5,
low=cv_min, high=cv_95)
mapper.low_color = 'blue'
mapper.high_color = 'red'
add_color_bar = True
ninety_degrees = pd.np.pi / 2.
color_bar = ColorBar(color_mapper=mapper, title='',
#title=color.value.title(),
title_text_font_style='bold',
title_text_font_size='20px',
title_text_align='center',
orientation='vertical',
major_label_text_font_size='16px',
major_label_text_font_style='bold',
label_standoff=8,
major_tick_line_color='black',
major_tick_line_width=3,
major_tick_in=12,
location=(0,0))
else:
c = ['#31AADE'] * len(xs)
add_color_bar = False
if add_color_bar:
source = ColumnDataSource(data=dict(x=xs, y=ys,
c=coloring, size=sz, name=name))
else:
source = ColumnDataSource(data=dict(x=xs, y=ys, color=c,
size=sz, name=name))
if add_color_bar:
p.circle('x', 'y', fill_color={'field': 'c',
'transform': mapper},
line_color=None, size='size', source=source)
else:
p.circle('x', 'y', color='color', size='size', source=source)
p.add_tools(HoverTool(tooltips=[('x', '@x'), ('y', '@y'),
('desc', '@name')]))
if add_color_bar:
color_bar_label = Label(text=color.value.title(),
angle=ninety_degrees,
text_color='black',
text_font_style='bold',
text_font_size='20px',
x=25, y=300,
x_units='screen', y_units='screen')
p.add_layout(color_bar, 'right')
p.add_layout(color_bar_label, 'right')
return p
def update(attr, old, new):
layout.children[1] = create_figure()
x = Select(title='X-Axis', value='mpg', options=columns)
x.on_change('value', update)
y = Select(title='Y-Axis', value='hp', options=columns)
y.on_change('value', update)
size = Select(title='Size', value='None',
options=['None'] + quantileable)
size.on_change('value', update)
color = Select(title='Color', value='None',
options=['None'] + quantileable)
color.on_change('value', update)
controls = widgetbox([x, y, color, size], width=200)
layout = row(controls, create_figure())
curdoc().add_root(layout)
curdoc().title = "Crossfilter"
最佳答案
您可以通过在单独的轴上绘制垂直标签并向该轴添加标题来向颜色栏添加垂直标签。为了说明这一点,这里是 Bokeh 标准 Colorbar 示例的修改版本(找到 here):
import numpy as np
from bokeh.plotting import figure, output_file, show
from bokeh.models import LogColorMapper, LogTicker, ColorBar
from bokeh.layouts import row
plot_height = 500
plot_width = 500
color_bar_height = plot_height + 11
color_bar_width = 180
output_file('color_bar.html')
def normal2d(X, Y, sigx=1.0, sigy=1.0, mux=0.0, muy=0.0):
z = (X-mux)**2 / sigx**2 + (Y-muy)**2 / sigy**2
return np.exp(-z/2) / (2 * np.pi * sigx * sigy)
X, Y = np.mgrid[-3:3:100j, -2:2:100j]
Z = normal2d(X, Y, 0.1, 0.2, 1.0, 1.0) + 0.1*normal2d(X, Y, 1.0, 1.0)
image = Z * 1e6
color_mapper = LogColorMapper(palette="Viridis256", low=1, high=1e7)
plot = figure(x_range=(0,1), y_range=(0,1), toolbar_location=None,
width=plot_width, height=plot_height)
plot.image(image=[image], color_mapper=color_mapper,
dh=[1.0], dw=[1.0], x=[0], y=[0])
color_bar = ColorBar(color_mapper=color_mapper, ticker=LogTicker(),
label_standoff=12, border_line_color=None, location=(0,0))
color_bar_plot = figure(title="My color bar title", title_location="right",
height=color_bar_height, width=color_bar_width,
toolbar_location=None, min_border=0,
outline_line_color=None)
color_bar_plot.add_layout(color_bar, 'right')
color_bar_plot.title.align="center"
color_bar_plot.title.text_font_size = '12pt'
layout = row(plot, color_bar_plot)
show(layout)
关于Bokeh Colorbar 颜色条右侧的垂直标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46841919/
我用 bokeh.plotting.Figure.line 画了下面的图。我如何添加垂直引用线来强调 Feb/14 的一个点? 这是另一个情节。这是 bokeh.charts.Bar。我想添加水平指南
我使用 python 在 Bokeh 中创建了一个散点图。我想创建一个小部件,允许用户更改散点图中点的颜色。有没有简单的方法可以做到这一点?我找到了一个基于 JQuery 的颜色选择器小部件 ( ht
我有一个 bokeh 应用程序,我可以在其中读取各种文件(6 到 12 个)并生成 DataTable 来比较几个不同的变量。 所以我有一个生成 DataTable 的循环,将它们添加到列表中,然后最
典型的 Bokeh 散点图可能如下所示: 其中 x 和 y 限制均为 [0,1]。如何删除轴上 0 之前并延伸到 1 之外的额外空间?这样最终图的左角为坐标 (0,0),左上角和右下角分别为 (0,1
我正在尝试创建一个 Bokeh 图,它允许我通过单击复选框来切换图中线条的可见性。 我开始尝试对复选框组使用 js 回调,但不幸的是,实际上并没有为复选框组实现 js 回调。 任何人都可以提出另一种方
例如,我有 button = Button(label="0", type="success") 选择此按钮后,我想将标签更改为“1”,反之亦然。有没有简单的方法来实现这一目标? 编辑: RadioB
我团队中的一些人,包括我自己,发现在 Bokeh 散点图中使用 circle 非常令人迷惑。方法,对于图中数据的初始自动缩放拟合,我们可以为我们的数据拨入合理的大小,例如使用 plot.circle(
所以我想做的是一个带有线条和圆圈的简单图形 http://docs.bokeh.org/en/latest/docs/quickstart.html#getting-started 但带有在鼠标悬停在
考虑一个垂直堆积条形图,其中每一列都由多个条形(段)组成。是否可以在每个段上添加工具提示?目前,相同的工具提示附加到组成该列的所有段。 from bokeh.core.properties impor
In[21]: from bokeh.charts import Bar, output_file, show In[22]: dict = {'sec': {u'A': 10, u'B': 20}}
在 Bokeh 中实现树形图需要什么?类似于这个 DS 示例的东西会很好—— http://bl.ocks.org/robschmuecker/7880033 我只是想可视化一个数据结构,所以只需要平
我了解如何指定在 Bokeh 中显示的特定刻度,但我的问题是是否有一种方法可以分配特定的标签来显示与位置。例如 plot.xaxis[0].ticker=FixedTicker(ticks=[0,1]
有什么方法可以为图例添加标题,例如左侧为当前图例,右侧为带有标题的图例。这个特殊的图例被放置在情节之外,所以我不知道是否可以用文本字形来伪造它。 最佳答案 从最新版本的 Bokeh (1.2) 开始,
问题:我的 python 应用程序启动了一个 Bokeh 服务器,如本文所述: http://matthewrocklin.com/blog/work/2017/06/28/simple-bokeh-
我正在使用 Bokeh 图制作折线图,我想标记实际的点(用线穿过它们),而不是仅仅标记线本身。我怎样才能做到这一点?我浏览了文档和用户指南,但似乎找不到答案。谢谢! 最佳答案 假设您正在使用 boke
我将一个 Bokeh Figure 实例作为输入传递给另一个函数,我需要在那里修改它的源代码。 不知道该怎么做! 这就是我创建图形实例的方式: source = ColumnDataSource({'
据我所知,我可以通过设置 responsive=True 来使 Bokeh 图响应 div 的宽度。然而,我感兴趣的是让 Bokeh 图只在一定程度上做出响应。有没有一种方法可以让我设置 Bokeh
我有一个 Bokeh 服务器应用程序。我想在命令行上传递自定义选项: bokeh serve /path/to/script.py --my-option foo 这可能吗? Bokeh 会以某种方式
我有一个图,它使用来自一些原始数据的值(在 0-20 的 x/y 范围内),以及一些导致一些相当大的 x/y 值(例如 -1000 到 1000+)的最小化数据。因此,我所做的绘图需要我手动单击缩放以
在Web应用程序中,我想让用户使用bokeh的漂亮的框/套索选择工具来选择绘制图像中的感兴趣区域。我想接收选定的像素,以便在python中进行进一步的操作。 对于散点图,这类似于gallery,很容易
我是一名优秀的程序员,十分优秀!