散点图的Python散景色标

时间:2016-06-13 13:54:34

标签: bokeh scatter

我正在使用Python库散景,并想知道是否可以使用散点图连续色标(或彩条)。

目前,有一个带有颜色组的图例,但不是像热图一样的连续色标,这很简单。

请帮助吗?

1 个答案:

答案 0 :(得分:1)

以下是散景中调色板的讨论:Custom color palettes with the image glyph

请注意底部的代码片段,介绍如何使用matplotlib色彩图创建散景调色板。

但是,我发现直接从matplotlib色彩映射创建单独的颜色通道更方便:

import numpy as np
import matplotlib.cm as cm

import bokeh.plotting as bk

# generate data
N = 4000
x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5

# get a colormap from matplotlib
colormap =cm.get_cmap("gist_rainbow") #choose any matplotlib colormap here

# define maximum and minimum for cmap
colorspan=[40,140]

# create a color channel with a value between 0 and 1
# outside the colorspan the value becomes 0 (left) and 1 (right)
cmap_input=np.interp(np.sqrt(x*x+y*y),colorspan,[0,1],left=0,right=1)

# use colormap to generate rgb-values
# second value is alfa (not used)
# third parameter gives int if True, otherwise float
A_color=colormap(cmap_input,1,True)

# convert to hex to fit to bokeh
bokeh_colors = ["#%02x%02x%02x" % (r, g, b) for r, g, b in A_color[:,0:3]]

# create the plot-
p = bk.figure(title="Example of importing colormap from matplotlib")

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

bk.output_file("rainbow.html")

bk.show(p)  # open a browser

我希望这有帮助!