2016-06-13 115 views
1

我正在使用Python库散景,并想知道是否可以使用散点图获得连续的色阶(或颜色条)。用于散点图的Python散景色比例尺

目前,使用颜色组的图例非常简单,但不像热图中那样具有连续的颜色比例。

请帮忙吗?

回答

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 

我希望这有助于!

+0

嗨@Anadyn,非常感谢有用的代码,我们可以添加一个连续的彩条? – french

+0

嗨@法国人,我想你必须手工做,看看这个[讨论](http://stackoverflow.com/questions/32614953/can-i-plot-a-colorbar-for-a-bokeh-heatmap ) – Anadyn