2017-10-09 124 views
0

我尝试在Python 3.5.3中使用Plotly制作热图。这个想法是传递网格上每个点的坐标(x,y),并根据属性z(z有三个值 - 0,1,2)对它们着色。Colorscale不匹配颜色(python)

from plotly import offline as py 

colors = [ 
    [0, 'rgb(0,0,255)'], #blue 
    [1, 'rgb(255, 0, 0)'], #red 
    [2, 'rgb(188, 188, 188)'] #gray 
] 

data = [dict(z=z, y=y, x=x, type='heatmap', colorscale=colors, showscale=True)] 

py.plot({'data': data, 'layout': {'width': 500, 'height': 500}}, filename="plot.html") 

但是,在结果图中,颜色完全不匹配。我尝试搜索Plotly文档,但仍然不知道这里有什么问题。

enter image description here

回答

0

documentation

的colorscale必须是含阵列阵列映射的归一化值 到RGB,RGBA,六角,HSL,HSV,或命名颜色字符串。最小值为 ,最低(0)和最高(1)值的映射为 。例如,[[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']]

在您的示例colorscale范围为0〜2。如果你把它归到最高1,它应该工作。

from plotly import offline as py 

colors = [[0, 'rgb(0,0,255)'], #blue 
      [0.5, 'rgb(255, 0, 0)'], #red 
      [1, 'rgb(188, 188, 188)'] #gray 
     ] 
z = [[1, 20, 30], 
    [20, 1, 60], 
    [30, 60, 1]] 
data = [dict(z=z, 
      type='heatmap', 
      colorscale=colors, 
      showscale=True)] 

py.plot({'data': data}) 

enter image description here

+0

非常感谢您!另外,如果我理解正确的值之间的时间间隔应该是大致相等的,对吗? – Boddha

+0

@Boddha:时间间隔取决于你,取决于你想得到什么。 –