将色图与bokeh散布一起使用 [英] Using colormap with bokeh scatter

查看:105
本文介绍了将色图与bokeh散布一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

matplotlib中, scatterplot 提供了使用绘图的颜色,以指示类似该绘图的值或幅度:

In matplotlib the scatterplot offers the possibility of using the color of a plot to indicate value or magnitude like this plot:

对于bokeh,类似的示例似乎是手动的生成rgb颜色,这使得很难生成颜色按幅度(特别是esp)缩放的图. wrt.不同的颜色图.

For bokeh, similar examples seem to manually generate the rgb colors, which makes it difficult to produce plots with color scaled by magnitude, esp. wrt. diverging colormaps.

bokeh中是否可能具有类似的功能,或使用matplotlib色彩映射表设置颜色?

Is it possible to have similar functionality in bokeh, or to use matplotlib colormaps to set the color?

推荐答案

直接使用matplotlib的颜色图很容易.例如,以下在bokeh的示例中使用viridis(请注意,我正在使用jupyter笔记本):

It's easy enough to just use matplotlib's colormaps directly. For example, the following uses viridis in bokeh's example (note that I'm using a jupyter notebook):

import numpy as np

from bokeh.plotting import figure, show, output_notebook
import matplotlib as mpl

output_notebook()

N = 4000
x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5
colors = [
    "#%02x%02x%02x" % (int(r), int(g), int(b)) for r, g, b, _ in 255*mpl.cm.viridis(mpl.colors.Normalize()(radii))
]

p = figure()

p.scatter(x, y, radius=radii,
          fill_color=colors, fill_alpha=0.6,
          line_color=None)

show(p)  

本质上,对于cm中的任何matplotlib颜色图,使用值数组进行初始化将返回一个数组,其中每个值都替换为[r,g,b,a]值,范围为[0,1].请注意,这也假设所有值都在0到1之间;在这里,我使用matplot.colors.Normalize来确保这一点.

Essentially, for any matplotlib colormap in cm, initializing it with an array of values will return an array with each value replaced by [r,g,b,a] values in the range [0,1]. Note that this assumes all the values are between 0 and 1 as well; here I use matplot.colors.Normalize to ensure this.

这篇关于将色图与bokeh散布一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆