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()
What I want is the following:
A sample of my data frame:
a b c d e f
DATE
2010-04-12 0 1 0 0 0 0
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()
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()