question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] go.Heatmap making values NaN? px.imshow is correct

See original GitHub issue

Issue: For some reason, using go.Heatmap causes values to be dropped/excluded from the plot. I’m guessing they’re being made NaN and filtered out somehow? px.imshow correctly includes all values.

See here for the output from the code below: output

Versions: pandas: 1.40 plotly: 5.8.0

Reproducible code:

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

df = pd.DataFrame([[438, 160, 2, 3, 2]], index=["AB"], columns=["100015", "500015", "500030", "500039", "500078"])
print(df)
fig1 = go.Figure(
    data=go.Heatmap(z=df.values, x=df.index, y=df.columns, colorbar=dict(title="count"), hoverongaps=False)
)
fig1.show()
fig2 = px.imshow(
    df.values.T,
    labels=dict(color="count"),
    x=df.index,
    y=df.columns,
)
fig2.show()

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolaskruchtencommented, May 27, 2022

It would be very challenging to implement this kind of warning at the graph_objects level unfortunately… That said, px.imshow actually does provide this warning (try px.imshow(df.values, x=df.index, y=df.columns)) and this kind of thing is one of the reasons it’s generally recommended to use px wherever possible 😃

0reactions
kmcentushcommented, May 27, 2022

Oh, I finally get it, thank you so much. Setting the axis names with different dimensions than the raw data cuts things off.

Do you think it’s worth adding some sort of warning when this happens (i.e. axis dimensions not aligning with input data)? Happy to close the issue if not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I plot NaN values as a special color with imshow in ...
I am trying to use imshow in matplotlib to plot data as a heatmap, but some of the values are NaNs. I'd like...
Read more >
Issues - GitHub
[BUG] Go.Heatmap()/ px.imshow() breaks when mixing ints with strings, even if converting #2530. Open. Juanouo opened this issue on Jun 2, ...
Read more >
How to deal with Heatmap NaN value - Plotly Community Forum
I have successfully using x, y, z value to create a heatmap. However, in my data, some have x, y value but did...
Read more >
Creating annotated heatmaps — Matplotlib 3.6.2 documentation
Matplotlib's imshow function makes production of such plots particularly easy. The following examples show how to create a heatmap with annotations.
Read more >
[Solved]-How can we plot data from a multi-index dataframe in ...
T # now restructure it for plotly dfp = df.stack("Venue").droplevel(0,0) # finally a simple plotly figure... px.imshow(dfp.values, x=dfp.columns, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found