Skip to content

[BUG] Go.Heatmap()/ px.imshow() breaks when mixing ints with strings, even if converting #2530

@Juanouo

Description

@Juanouo

Thank you so much for helping improve the quality of Dash!

We do our best to catch bugs during the release process, but we rely on your help to find the ones that slip through.

Describe your context
Please provide us your environment so we can easily reproduce the issue.

  • replace the result of pip list | grep dash below
dash==1.10.0
dash-bootstrap-components==0.9.2
dash-core-components==1.9.0
dash-extensions==0.0.3
dash-html-components==1.0.3
dash-renderer==1.3.0
dash-table==4.6.2
  • if frontend related, tell us your Browser, Version and OS

    • OS: [Windows 10]
    • Browser [Chrome]
    • Version [83]

Describe the bug

If x axis or y axis labels contain both ints and strings, even if ints are transformed to string, heatmap/imshow breaks and brings an incorrect result. In my case I get a 3x3 heatmap with float labels, when I'm looking for a 5x5 heatmap with string labels (screenshot below).

Specifically I had labels "1", "2", "3", "4", "5 or more" in my heatmap which breaks it. If I change it to "1d", "2d", "3d", "4d", "5 or more" it doesn't break anymore. Because I'm using pivot tables it's a bit hard to give code that can instantly replicate the issue, but I'll add the important bits.

This doesn't work:

    df.bathrooms = (np.where(df.bathrooms < 5,
                             df.bathrooms.astype(int).astype(str)+'b',
                             '5 or more'))
    df.dorms = np.where(df.dorms < 5, df.dorms.astype(int).astype(str)+'d',
                        '5 or more')
    pivot = pd.pivot_table(df, values='cap_rate', index='bathrooms',
                           columns='dorms', aggfunc='mean')
    print(pivot.to_dict())
    fig = px.imshow(pivot.values,
                    
                    x=pivot.columns, y=pivot.index)

This works:

df.bathrooms = (np.where(df.bathrooms < 5,
                         df.bathrooms.astype(int).astype(str)+'b', # Added +'b' here
                         '5 or more'))
df.dorms = (np.where(df.dorms < 5,
                     df.dorms.astype(int).astype(str)+'d', # Added +'d' here
                     '5 or more'))

pivot = pd.pivot_table(df, values='cap_rate', index='bathrooms', columns='dorms', aggfunc='mean')
print(pivot.to_dict())
fig = px.imshow(pivot.values,
                
                x=pivot.columns, y=pivot.index)

The only difference is I turned the ints into proper strings by adding them a letter. astype(str) won't be enough.

Expected behavior

Heatmap should present correctly.

Screenshots

Working heatmap with getaround method:

image

Original bugged heatmap:

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions