2016-06-13 116 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