1

I have generated a heatmap using px.imshow


returns = returns.pivot('year', 'month', 'tradegain').fillna(0)
returns = returns\[\['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'\]\]
returns\['Total returns'\] = dayret.groupby(dayret.index.year).tradegain.sum().values
returns = round(returns,2)
fig = px.imshow(returns,text_auto=True,aspect='auto',title=title,color_continuous_scale= px.colors.sequential.BuGn_r)

I want negative returns to be displayed in red and everything else in green. Is there any way possible to do that?

I have also tried making my own color scheme

fig = px.imshow(returns,text_auto=True,aspect='auto',title=title,color_continuous_scale=['red','yellow','green','green',                                                                                              'green','green','green','green','green','green','green','green','green','green','green']) 

also with

fig = px.imshow(returns,text_auto=True,aspect='auto',title=title,color_continuous_scale=[[0.0,'Red'],[0.5,'Free ']])

no good result

I want the result to be visually appealing.

Hamzah
  • 8,175
  • 3
  • 19
  • 43
Anirudh B M
  • 55
  • 1
  • 6
  • maybe this [answer](https://stackoverflow.com/a/52969848/10452700) or this [answer](https://stackoverflow.com/a/50104798/10452700) helps to smoothen – Mario Nov 11 '22 at 05:47

1 Answers1

1

You should create your own colorscale. Because there is no data for return in the question. I give you an example below:

import plotly.express as px
import numpy as np

z = np.array([[.1, .3, .5, .7, -.9],
     [1, .8, 60, -10, -.2],
     [.2, 0, -.5, .7, .9],
     [.9, .8, -.4, .2, 0],
     [.3, .4, -.5, .7, 1]])

maxz = np.max(z)+0.1
minz = np.min(z)

colorscale = ["green" if i>=0 else 'red' for i in np.arange(minz,maxz,0.01)]

fig = px.imshow(z, text_auto=True, aspect="auto", color_continuous_scale=colorscale)
fig.show()

enter image description here

Hamzah
  • 8,175
  • 3
  • 19
  • 43