I am doing Spotify data analysis. I would like to create a correlation matrix using Plotly. My sample data frame as below (dataframe name called 'df')
I created a correlation matrix by using below code
import plotly.offline as pyo
import plotly.express as px
import plotly.graph_objs as go
import numpy as np
import pandas as pd
trace = go.Heatmap(z=df.values,x=df.index.values,y=df.columns.values,colorscale='RdBu_r',
zmax=1,zmin=-1,text=corr.values)
fig = go.Figure(data=[trace])
pyo.plot(fig)
fig.show()
Getting below output
I need to get a clear correlation matrix output, can anyone advise how to do this in Plotly? Unable to fix the above code in order to get the right output
CodePudding user response:
I think we can use df.corr()
in pandas to calculate the correlation coefficient and make a heatmap of it.
import plotly.graph_objects as go
import random
import numpy as np
df = pd.DataFrame({'danceability': np.random.rand(100),
'energy': np.random.rand(100),
'loudness': np.random.rand(100),
'mode': random.choices([0,1], k=100)})
df_corr = df.corr()
print(df_corr)
danceability energy loudness mode
danceability 1.000000 0.061570 0.048222 -0.060654
energy 0.061570 1.000000 0.019930 0.042376
loudness 0.048222 0.019930 1.000000 -0.044294
mode -0.060654 0.042376 -0.044294 1.000000
fig = go.Figure()
fig.add_trace(
go.Heatmap(
x = df_corr.columns,
y = df_corr.index,
z = np.array(df_corr),
text=df_corr.values,
texttemplate='%{text:.2f}'
)
)
fig.show()