bluprince13
bluprince13

Reputation: 5001

How to get the full range of a colorscale?

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.

Upvotes: 1

Views: 1479

Answers (2)

JBa
JBa

Reputation: 29

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()

Upvotes: 0

bluprince13
bluprince13

Reputation: 5001

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.

Upvotes: 1

Related Questions