plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
16.2k stars 2.55k forks source link

Imshow behaviour incorrect with log scale on yaxis #4284

Open luigibrancati opened 1 year ago

luigibrancati commented 1 year ago

Plotly version: 5.15.0

I'm passing some values to the yaxis of px.imshow. If I just plot it as-is it's fine

import plotly.express as px
fig = px.imshow(
    img = [[1,2], [3,4], [5,6]],
    x = ['A', 'B'],
    y = [10, 100, 1000],
    text_auto=True,
    width=600
)
fig.update_yaxes(autorange='reversed')

immagine

But if I change the yaxis type to log using fig.update_yaxis, the first row of data disappears

import plotly.express as px
fig = px.imshow(
    img = [[1,2], [3,4], [5,6]],
    x = ['A', 'B'],
    y = [10, 100, 1000],
    text_auto=True,
    width=600
)
fig.update_yaxes(type='log', autorange='reversed')

immagine

Playing around with the yaxis values doesn't seem to affect this behaviour

immagine

Note: It seems to depend only on the closeness of the first and second yaxis values

immagine

VS

immagine

luigibrancati commented 1 year ago

One possible solution to this is to add a fictitious data point as a first row, like the 0 I added here

immagine

alexcjohnson commented 1 year ago

We could probably do something better here in the log case, but fundamentally the issue is image / heatmap data has bricks with some implied extent if you specify one coordinate value per brick, and this is particularly awkward when those coordinate values are not distributed linearly. If you give us a coordinate array (y in this case) that's one longer than the corresponding dimension of the img array (or z if you use heatmap directly) we'll treat those as the brick edges rather than centers, and then there's no ambiguity - although then there's ambiguity in where we should draw the text and what values we should display on hover!