My Y axis doesnt show all the dates on Y axis while plotting heatmap in plotly

Question:

I know how to make a good heatmap in seaborn, but I would like to use the Plotly package. I used the code below, but my Y axis doesn’t show all the dates, how can I show all the the dates on my y axis?

I want background color be white and each category have different custom color too, how should I modify the code?

My data frames includes a date column and other columns which I would put them on X axis

import plotly.express as px

fig = px.imshow(df, text_auto=True,color_continuous_scale='RdBu_r', origin='lower')


fig.show()

enter image description here

What I want is the following:

enter image description here

A sample of my data frame:


            a   b   c   d   e   f
DATE                        
2010-04-12  0   1   0   0   0   0

Chart based on code provided as below:
enter image description here

Asked By: user14269252

||

Answers:

Ticks can be changed to any desired value by manually updating the layout.
To show all the dates and avoid being automatically compressed by Plotly, you can pass the dates to yaxis as string.

Regarding the background color, it is controlled by the color scale. Set color_continuous_scale as blues will probably give you a white background.

fig = px.imshow(df.values, color_continuous_scale='blues',
                aspect='auto', width=500, height=600)
fig.update_layout(
    yaxis = dict(
        tickmode = 'array',
        tickvals = np.arange(len(df)),
        ticktext = df.index.astype(str)
    ),
    xaxis = dict(visible = False)
)
fig.show()

result image

If the heat map is too long to show all the dates, just skip some of them.

fig = px.imshow(df_long.values, color_continuous_scale='blues',
                aspect='auto', width=500, height=600)
n_skip = 10
fig.update_layout(
    yaxis = dict(
        tickmode = 'array',
        tickvals = np.arange(0, len(df_long), n_skip), # skip here
        ticktext = df_long.index.astype(str)[::n_skip] # skip here
    ),
    xaxis = dict(
        tickvals = np.arange(df_long.shape[1]),
        ticktext = ['a', 'b', 'c', 'd', 'e', 'f']
    )
)
fig.update_coloraxes(showscale=False)  # remove color bar
fig.show()

result image long without color bar

Answered By: Cheng
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.