Search code examples
pythonlayoutannotationsplotlyheatmap

Annotation bug coercing string to float


I've encountered what I think is a bug with create_annotated_heatmap. I'm making a plot with annotation text with one value in the annotation that is a string but can be coerced into a float. The following should be reproducible

import plotly.figure_factory as ff
ff.create_annotated_heatmap(
        x=['x'], 
        y=['1491', 'ab', 'cd', 'ef'], 
        z=[[1], [2], [3], [4]],
        annotation_text=[['1491'], ['ab'], ['cd'], ['ef']])

The output looks awful because the first y value gets passed into annotations in layout with a value of '1491' but is coerced into 1491, while the other values are treated as categories and then mapped to small integers. enter image description here

I've tried assigning the heatmap to fig and running fig.update_yaxes(type='category') and various permutations around that. Nothing works. Is there any way to get around this?


Solution

  • you need to comment out the y=[...] and it will work. annotation_text is fine. You can check out more options if you want to have a matrix/heatmap here. Assuming this is what you are expecting...

    import plotly.figure_factory as ff
    ff.create_annotated_heatmap(
            x=['x'], ## Label on top
            #y=['1491', 'ab', 'cd', 'ef'], ##Comment out as you have just one dimension in x
            z=[[1], [2], [3], [4]], ## 4 horizontal bars, no matrix/heatmap
            annotation_text=[['1491'], ['ab'], ['cd'], ['ef']]) ## Text inside each bar
    

    Output plot

    enter image description here