plot - 如何使用 Python 3 使用连续颜色图在 `Heatmaps` 中制作 `Bokeh` ?

标签 plot colors widget heatmap bokeh

我试图复制这种将连续值映射到 LinearColorMapper 实例的 HeatMap 样式:http://docs.bokeh.org/en/latest/docs/gallery/unemployment.html 我想制作一个 HeatMap (带有 chartsrect),然后添加 single selection widget选择 obsv_id,然后选择 slider widget浏览日期

但是,一开始我在使用 HeatMap 本身和单个 obsv_id/date 对时遇到了麻烦。我在创建此 HeatMap 时做错了什么?这本质上是 size 变量和 loc 变量的 3x3 矩形图。

奖励:您能帮助我/提供一些关于如何连接这些小部件的输出来控制绘图的建议吗?

我看到了这些帖子,但所有示例都使用实际的十六进制颜色作为列表,而不是使用连续度量进行映射: python bokeh, how to make a correlation plot? http://docs.bokeh.org/en/latest/docs/gallery/categorical.html

# Init
import numpy as np
import pandas as pd
from bokeh.plotting import figure, output_notebook, output_file, reset_output, show, ColumnDataSource
from bokeh.models import LinearColorMapper
reset_output()
output_notebook()

np.random.seed(0)

# Coords
dates = ["07-3","07-11","08-6","08-28"]
#locs = ["air","water","earth"]
locs = [0,1,2]
size = [3.0, 0.2, 0.025]
observations = ["obsv_%d"%_ for _ in range(10)]


# Data
Ar_tmp = np.zeros(( len(dates)*len(locs)*len(size)*len(observations), 5 ), dtype=object)

i = 0
for date in dates:
    for loc in locs:
        for s in size:
            for obsv_id in observations:
                Ar_tmp[i,:] = np.array([obsv_id, date, loc, s, np.random.random()])
                i += 1
DF_tmp = pd.DataFrame(Ar_tmp, columns=["obsv_id", "date", "loc", "size", "value"])
DF_tmp["value"] = DF_tmp["value"].astype(float)
DF_tmp["size"] = DF_tmp["size"].astype(float)
DF_tmp["loc"] = DF_tmp["loc"].astype(float)
#     obsv_id   date    loc   size     value
# 0    obsv_0   07-3    air    3.0  0.548814
# 1    obsv_1   07-3    air    3.0  0.715189
# 2    obsv_2   07-3    air    3.0  0.602763
# 3    obsv_3   07-3    air    3.0  0.544883
# 4    obsv_4   07-3    air    3.0  0.423655

mapper = LinearColorMapper(low = DF_tmp["value"].min(), high = DF_tmp["value"].max())

# # Create Heatmap of a single observation and date pair
query_idx = set(DF_tmp.index[DF_tmp["obsv_id"] == "obsv_0"]) & set(DF_tmp.index[DF_tmp["date"] == "08-28"])

# p = HeatMap(data=DF_tmp.loc[query_idx,:], x="loc", y="size", values="value")
p = figure()
p.rect(x="loc", y="size", 
       source=ColumnDataSource(DF_tmp.loc[query_idx,:]),
       fill_color={'field': 'value', 'transform': mapper},
       line_color=None)
show(p)

我的错误:

# Javascript error adding output!
# TypeError: Cannot read property 'length' of null
# See your browser Javascript console for more details.

最佳答案

您必须提供paletteLinearColorMapper。例如:

mapper = LinearColorMapper(
    palette='Magma256',
    low=DF_tmp["value"].min(),
    high=DF_tmp["value"].max()
)

来自LinearColorMapper doc :

class LinearColorMapper(palette=None, **kwargs)

Map numbers in a range [low, high] linearly into a sequence of colors (a palette).

<小时/>

与您的异常无关,但您还需要将 widthheight 参数传递给 p.rect()

关于plot - 如何使用 Python 3 使用连续颜色图在 `Heatmaps` 中制作 `Bokeh` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40054055/

相关文章:

matlab - 格林函数的傅里叶变换 - 两个信号之间的差异几乎相同

python - 在 python 中的 matplotlib 中的 logscale 上绘制问题

Javascript/jQuery 图例小部件

c++ - Qt 小部件的命名约定

javascript - Photoshop 用于匹配图像颜色的算法

flutter - 获取 "The argument type ' Widget ?' can' t 被分配给参数类型 'Widget' “错误

r - 在绘图轴的全局级别上强制 las=1

r - 无法用神经网络图生成PDF

linux - 如何从脚本的纯色图像中获取十六进制颜色代码?

r - 如何在ggplot2中获得水平颜色渐变?