英文:
My Y axis doesnt show all the dates on Y axis while plotting heatmap in plotly
问题
我知道如何在seaborn中创建一个良好的热力图,但我想要使用Plotly包。我使用了下面的代码,但我的Y轴不显示所有的日期,如何显示所有日期在我的Y轴上?
我想要背景颜色为白色,每个类别都有不同的自定义颜色,我应该如何修改代码?
我的数据框包括一个日期列和其他列,我想将它们放在X轴上。
import plotly.express as px
fig = px.imshow(df, text_auto=True, color_continuous_scale='RdBu_r', origin='lower')
fig.show()
我想要的效果如下图所示:
我的数据框的一个示例:
a b c d e f
DATE
2010-04-12 0 1 0 0 0 0
基于提供的代码的图表如下:
英文:
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
答案1
得分: 2
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.
如果要手动更改刻度,可以通过手动更新布局来更改刻度。要显示所有日期并避免被Plotly自动压缩,可以将日期作为字符串传递给y轴。
关于背景颜色,它受颜色比例的控制。将color_continuous_scale
设置为blues
可能会给您一个白色背景。
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()
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论