1

I'm generating a heatmap using plotly and then changing the colorscale. If the values of the heatmap are all positive, this then results in a colorscale that doesn't span the full range of color.

How can I fix this?

Input

import pandas as pd
import numpy as np
import plotly.offline as py
import cufflinks as cf
cf.go_offline()

index = range(500)
columns = range(20)
df = pd.DataFrame(np.random.randn(500, 20) + 100, index=index, columns=columns)

fig = df.iplot(kind='heatmap', colorscale='Rdbu', asFigure=True)
fig['data'][0].update(colorscale='Redblue')
py.iplot(fig)

Output

Image

See the interactive graph.

bluprince13
  • 4,607
  • 12
  • 44
  • 91
  • 2
    Look through the docs. There should parameters to specify the min and max values of the colormap. https://stackoverflow.com/q/3373256/2823755 might get you started. – wwii Oct 14 '17 at 23:18

2 Answers2

1

I haven't fully figured this one out, but plotly seems to select colorscales depending on whether the data consists of positive, negative, or mixed numbers.

In my case, when my numbers were all positive, I was getting a red only colorscale. Specifying the min and max didn't help.

To solve the problem, I defined my own colorscale with help from the plotly documentation.

bluprince13
  • 4,607
  • 12
  • 44
  • 91
0

One can define zmax and zmin with the maximal and minimal values in Heatmap to get the full range for the colorbar. In the following example the colobar ranges from 0 to 1.

import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Heatmap(z=[[0.25,0.5,0.75]],zmin=0,zmax=1))
fig.show()
JBa
  • 29
  • 4