i am trying to use plotly to create a heatmap that include 4 paramaeters for each subject in different category. But for some reasons, it does not show a complete heatmap for it (see figure).
The upper part is blank but it show contain colour blocks based on my Excel sheet
Here is the code:
import pandas as pd
import plotly.graph_objects as go
import numpy as np
pd.set_option('display.max_rows', None)
# Load the data from the new Excel file
df = pd.read_excel(r'C:UsersAcerOneDriveDesktopBook10.xlsx')
# Fill forward the 'Disease' column to handle the NaN values correctly
df['Disease'] = df['Disease'].fillna(method='ffill')
df['BC Score'] = df['BC Score'].fillna(np.nan)
df['CC Score'] = df['CC Score'].fillna(np.nan)
df['MCC Score'] = df['MCC Score'].fillna(np.nan)
# Define the x-axis genes and y-axis centrality measures
genes = df['Gene'].unique()
diseases = df['Disease'].unique()
centrality_measures = ['Degree', 'BC', 'CC', 'MCC']
# Create matrices for each centrality measure
degree_matrix = df.pivot_table(index='Gene', columns='Disease', values='Degree Score')
bc_matrix = df.pivot_table(index='Gene', columns='Disease', values='BC Score')
cc_matrix = df.pivot_table(index='Gene', columns='Disease', values='CC Score')
mcc_matrix = df.pivot_table(index='Gene', columns='Disease', values='MCC Score')
# Create a list of all combinations of diseases and centrality measures for the x-axis
x_labels = []
for disease in diseases:
x_labels.extend([f"{disease} Degree", f"{disease} BC", f"{disease} CC", f"{disease} MCC"])
# Create the figure
fig = go.Figure()
# Add Degree Score Heatmap
fig.add_trace(go.Heatmap(
z=degree_matrix,
x=[f"{disease} Degree" for disease in diseases],
y=genes,
colorscale='Viridis',
colorbar=dict(title='Degree Score', x=0.22, y=-1),
showscale=True
))
# Add BC Score Heatmap
fig.add_trace(go.Heatmap(
z=bc_matrix,
x=[f"{disease} BC" for disease in diseases],
y=genes,
colorscale='Cividis',
colorbar=dict(title='BC Score', x=0.45, y=-1),
showscale=True
))
# Add CC Score Heatmap
fig.add_trace(go.Heatmap(
z=cc_matrix,
x=[f"{disease} CC" for disease in diseases],
y=genes,
colorscale='Inferno',
colorbar=dict(title='CC Score', x=0.68, y=-1),
showscale=True
))
# Add MCC Score Heatmap
fig.add_trace(go.Heatmap(
z=mcc_matrix,
x=[f"{disease} MCC" for disease in diseases],
y=genes,
colorscale='Magma',
colorbar=dict(title='MCC Score', x=0.91, y=-1),
showscale=True
))
# Update layout to include a secondary y-axis for diseases
fig.update_layout(
yaxis=dict(
title='Gene',
tickvals=list(range(len(genes))),
ticktext=genes
),
xaxis=dict(
title='Disease and Centrality Measure',
tickvals=list(range(len(x_labels))),
ticktext=x_labels,
tickangle=-45
),
title="Heatmap of Centrality Measures by Disease",
height=1400,
width=2000,
margin=dict(l=100, r=200, b=200)
)
# Show the plot
fig.show()
Anyone knows how to solve it?
I had tried filling up the missing values with NaN but it still looks the same.
New contributor
ihatebio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.