Search code examples
plotlyplotly-python

Plotly Express imshow - disable label interpolation


Creating a heatmap with an uneven number of rows and columns, year on the y axis and month on the x, like so:

fig = px.imshow(
rounded_heatmap, 
text_auto=True,
color_continuous_scale=["rgb(227,237,224)", 'rgb(52,87,94)'],
template='mytemplate',
x=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug','Sep', 'Oct', 'Nov', 'Dec'],
y=[2019,2020,2021,2022], )

When this draws, it interpolates additional "between year" y-axis labels. I want to disable this behavior - can't locate how to do in the docs.

enter image description here


Solution

  • It can be improved by specifying the y-axis type as category. Modify some of the examples in the reference to fit your data and specify the y-axis type as categorical.

    import plotly.express as px
    data=[[1, 25, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, 5, 20]]
    fig = px.imshow(data,
                    labels=dict(x="Day of Week", y="Time of Day", color="Productivity"),
                    x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
                    y=[2020,2021,2022]
                   )
    fig.update_xaxes(side="top")
    fig.update_yaxes(type='category')
    fig.show()
    

    enter image description here