I have created a lower triangular correlation heatmap using Seaborn that I loved. Now trying to create the same using Plotly. Unfortunately, not being able to fine tune it like I did with Seaborn.
names = ['U', 'V', 'W', 'X', 'Y', 'Z']
r = pd.DataFrame(index = names, columns = names)
r['U'] = np.array([1.0, 0.53, 0.26, 0.63, 0.52, 0.65] )
r['V'] = np.array([0.53, 1.0, -0.17, 0.83, 1, 0.85])
r['W'] = np.array([0.26, -0.17, 1.0, 0.04, -0.15, 0.09])
r['X'] = np.array([0.63, 0.83, 0.04, 1, 0.83, 0.80])
r['Y'] = np.array([0.52, 1, -0.15, 0.83, 1, 0.86])
r['Z'] = np.array([0.65, 0.85, 0.09, 0.80, 0.86, 1.0])
print(r)

import seaborn as sns
# sns.set_theme(style="white")
mask = np.triu(np.ones_like(r, dtype=bool))
# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))
# Generate a custom diverging colormap
cmap = sns.diverging_palette(230, 20, n=256, as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(r,
mask=mask,
cmap=cmap,
vmax=1,
vmin = -.25,
center=0,
square=True,
linewidths=.5,
annot = True,
fmt='.2f',
annot_kws={'size': 10},
cbar_kws={"shrink": .75})
plt.title('Asset Correlation Matrix')
plt.tight_layout()
ax.tick_params(axis = 'x', labelsize = 8)
ax.set_ylim(len(corr)+1, -1)
# plt.savefig('corrTax.png', dpi = 600)
plt.show()

I am trying to create this using Plotly. Here is what I have able to do so far.
mask = np.triu(np.ones_like(r, dtype=bool))
rLT = r.mask(mask)
heat = go.Heatmap(
z = rLT,
x = rLT.columns.values,
y = rLT.columns.values,
zmin = - 0.25, # Sets the lower bound of the color domain
zmax = 1,
xgap = 1, # Sets the horizontal gap (in pixels) between bricks
ygap = 1,
colorscale = 'RdBu'
)
title = 'Asset Correlation Matrix'
layout = go.Layout(
title_text=title,
title_x=0.5,
width=600,
height=600,
xaxis_showgrid=False,
yaxis_showgrid=False,
yaxis_autorange='reversed'
)
fig=go.Figure(data=[heat], layout=layout)
fig.show()

annot option in seaborn), with rounding optionEasiest way I've found to remove top triangle in view
# Correlation
df_corr = data.corr().round(1)
# Mask to matrix
mask = np.zeros_like(df_corr, dtype=bool)
mask[np.triu_indices_from(mask)] = True
# Viz
df_corr_viz = df_corr.mask(mask).dropna(how='all').dropna('columns', how='all')
fig = px.imshow(df_corr_viz, text_auto=True)
fig.show()

Deb. Here it goes the answer for the first of your questions.
Seaborn colormap that I was create, I want to create something similar in Plotly. How can I do that?
You can use of the built-in colorscales in Plotly, which can be set via the parameter colorscale in the Heatmap constructor. Also, you can set Plotly's theme to get rid of the ugly background
import plotly.io as pio
import plotly.express as px
import plotly.graph_objects as go
pio.templates.default = "plotly_white"
go.Heatmap(
z=corr.mask(mask),
x=corr.columns,
y=corr.columns,
colorscale=px.colors.diverging.RdBu,
zmin=-1,
zmax=1
)

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With