Inverted image in go.Image vs go.Heatmap?

Hello, I have a figure with three subplots, where the third is overlay of the first and second. First subplot is created by go.Image, second by go.Heatmap. For some reason they have switched y axis, but the overlay is correct.

  1. Does go.Image or go.Heatmap invert the figure in y?
  2. How is it possible that the overlay is correct?

By px.imshow-ing both pictures, they are in correct orientation (the same as go.Image image and their overlay is shown).

test_merge = images_merged[0]
test_mask = masks[0]
fig = make_subplots(cols=3, rows=1, row_heights=[1])

# Independent pictures
fig = fig.add_trace(go.Image(z=test_merge*500, ), col=1, row=1, )
fig = fig.add_trace(
    go.Heatmap(
        z=test_mask,
        opacity=1,
        colorscale=['#000000'] + px.colors.qualitative.Alphabet * 40,
        showscale=False
#         coloraxis='coloraxis1'  # Specify a custom color axis for this trace
    ),
    col=2,
    row=1
)

# Overlay
fig = fig.add_trace(go.Image(z=test_merge*500), col=3, row=1)
fig = fig.add_trace(
    go.Heatmap(
        z=test_mask,
        opacity=0.5,
        colorscale=['#000000'] + px.colors.qualitative.Alphabet * 40,
        showscale=False
#         coloraxis='coloraxis2'  # Specify a custom color axis for this trace
    ),
    col=3,
    row=1,
)

# Update the color axes to hide the colorscale bar
fig = fig.update_layout(coloraxis1=dict(showscale=False))
fig = fig.update_layout(coloraxis2=dict(showscale=False))

fig = fig.update_layout(height=400)
# fig = fig.update_layout(yaxis=dict(autorange="reversed"))


fig.show()

Thanks a lot!

Yes, go.Heatmap() inverts the y-axis. Instead of go.Heatmap() you could use px.imshow() which has the parameter origin which alters this behaviour.

I don’t know :rofl:

EDIT: Quite helpful SO post:

1 Like

Thank you, I ended up with

fig.update_yaxes(autorange="reversed")

Which does the trick. I thought that

fig = fig.update_layout(yaxis=dict(autorange="reversed"))

does the same thing, but apparently my syntax was wrong in this line somehow (Plotly still confuses me a bit).