✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar! # Plotly Heatmap Z-Axis values need flipping, how to?

I’m trying to graph a heatmap of class period attendance by student by referencing a pandas dataframe “graph-me”. Here is the code for the Heatmap data

``````data= [
go.Heatmap(
x=['Period 1', 'Period 2', 'Period 3', 'Period 4', 'Period 5', 'Period 6', 'Period 7', 'Period 8', 'Period 9'],
y=graph_me['Student Name'],
z=[graph_me['Percentage P1'], graph_me['Percentage P2'], graph_me['Percentage P3'], graph_me['Percentage P4'], graph_me['Percentage P5'], graph_me['Percentage P6'], graph_me['Percentage P7'], graph_me['Percentage P8'], graph_me['Percentage P9']],
hoverongaps = False)
],
``````

The problem is that my Z values fill in horizontally when I expected them to fill in vertically. I have over 100 values on my y-axis and can’t manually change this. How do I flip the reference of my Z-values without changing my x or y axis?

I’m just learning Plotly, the curve has been steep so I hope my question is understandable

Hi @angelojc,
Welcome to Plotly forum!!!

I generated random data to illustrate how a heatmap that you intend to plot looks like.

If `Heatmap.x` has 9 elements (periods), and `Heatmap.y`, 100 student names, then z must be an array of 100 rows and 9 columns.

Please copy the code below, run it, and let us know if you want more details:

``````import numpy as np
from random import choice
import plotly.graph_objs as go

graph_me = {'Student Name': [f'Student {j}' for j in range(1, 101)]}

for k in range(1,10):
graph_me[f'Percentage P{k}']= np.random.randint(10, 60, 100)

#graph_me['Percentage P1']

z = np.array([graph_me[f'Percentage P{k}'] for k in range(1,10)]).T
print(z.shape)

heat = go.Heatmap(x=[f'Period {k}' for k in range(1,10)],
y=graph_me['Student Name'],
z=z, coloraxis='coloraxis')

fig = go.Figure(heat)
fig.update_layout(width=800, height=1400,
coloraxis =dict(colorscale='matter',
colorbar_thickness=25,
colorbar_title='%'),
yaxis_autorange='reversed',
xaxis_tickangle=-45)
fig.show()
``````

@empet thanks for the warm welcome and this framework!

I implemented a more crude solution but will study this for the future, I’ll probably need it. Here’s a picture of the final result

1 Like