plotly express heatmap cell size
Asked Answered
E

3

9

How can i change the size of cells in a plotly express heatmap? I would need bigger cells

import plotly.express as px
fig1 = px.imshow(df[col],color_continuous_scale='Greens')
fig1.layout.yaxis.type = 'category'
fig1.layout.xaxis.type = 'category'
fig1.layout.yaxis.tickmode = 'linear'
fig1.layout.xaxis.tickmode = 'linear'
fig1.layout.xaxis.tickangle = 65
fig1.layout.autosize = True
fig1.layout.height = 500
fig1.layout.width = 500

fig1.show()

Result (very narrow)

enter image description here

Eadith answered 6/10, 2020 at 22:41 Comment(1)
Can you please attach your dataframe or a sample dataframe similar to yours? Otherwise no one can reproduce your codePunak
M
7

'px' may not make it square due to the color bar, so why not use 'go'?

import plotly.graph_objects as go

fig = go.Figure(data=go.Heatmap(
                    z=[[1, 20, 30],
                      [20, 1, 60],
                      [30, 60, 1]]))

fig.show()

enter image description here

Set the graph size.

fig.layout.height = 500
fig.layout.width = 500

enter image description here

Examples at px

import plotly.express as px
data=[[1, 25, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, 5, 20]]
fig = px.imshow(data,
                labels=dict(x="Day of Week", y="Time of Day", color="Productivity"),
                x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
                y=['Morning', 'Afternoon', 'Evening']
               )
fig.update_xaxes(side="top")

fig.layout.height = 500
fig.layout.width = 500

fig.show()

enter image description here

Michaeu answered 7/10, 2020 at 3:31 Comment(2)
The cells are square, but plotly really lacks a way to set a cell width. Since we can only configure the graph width, the cell size varies according to the numer of cells. And since the graph width seems to include tick texts, legens, etc. one can't just set the width as a multiple of the number of cells.Coburn
Actually, your second example does not really have square cells. The total size is 336x320 in pixel dimensions.Opt
P
6

A little late, but if you want the cells to be wider, use this:

fig1 = px.imshow(df[col],color_continuous_scale='Greens', aspect='auto')

Setting the aspect argument to "auto" will fill the plotting area using non-square tiles.

Pyotr answered 22/6, 2022 at 2:21 Comment(0)
D
0

Using scaleanchors and constraining to [![enter image description here][1]][1]"domain" seem to do the trick, along with fixing the width and then scaling the height according to the number of rows and columns:


import plotly.graph_objects as go
import numpy as np

data = np.random.rand(8, 12)  # 8 rows and 12 columns

heatmap = go.Heatmap(
    z=data,
    colorscale='Viridis',
    colorbar=dict(thickness=20, ticklen=4)
)

fig = go.Figure(data=[heatmap])

# for aspect ratio
num_rows, num_cols = data.shape

# set some margins and padding
margin = 50
colorbar_width = 50

# fix the width, scale the height
fig_width = 600
fig_height = (fig_width - margin - colorbar_width) * num_rows / num_cols + margin

fig.update_layout(
    width=fig_width,
    height=fig_height,
    margin=dict(t=margin, b=margin, l=margin, r=margin + colorbar_width),
    xaxis=dict(scaleanchor="y", constrain="domain"),
    yaxis=dict(scaleanchor="x", constrain="domain")
)

fig.show()

A plotly heatmap with square cells

Duron answered 7/6, 2024 at 19:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.