python - 在绘图中绘制热图时,我的 Y 轴未显示 Y 轴上的所有日期

标签 python plotly heatmap

我知道如何在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()

enter image description here

我想要的是以下内容:

enter image description here

我的数据框示例:


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

基于提供的代码的图表如下: enter image description here

最佳答案

可以通过手动更新布局将刻度更改为任何所需的值。 为了显示所有日期并避免被 Plotly 自动压缩,您可以将日期作为字符串传递给 yaxis。

关于背景颜色,是由色阶控制的。将 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()

result image

如果热图太长而无法显示所有日期,只需跳过其中一些即可。

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

关于python - 在绘图中绘制热图时,我的 Y 轴未显示 Y 轴上的所有日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75686137/

相关文章:

python - 如何从 QLineEdit() 获取 int?

python - 绘制中断/断开的轴,x 轴上有间隙,指示刻度跳跃

python - dashplotly - 动态创建下拉菜单,选择多行,表格导出?

r - R 中的热图,行中出现重复名称

R:heatmap.2 更改颜色键

javascript - 如何创建Hotjar之类的应用程序?

python - django-import-export 覆盖不起作用

python - Tensorflow 1.10 TFRecordDataset - 恢复 TFRecord

R Markdown 和 Plotly : fig. 对齐不适用于 HTML 输出

python - 合并 Conv2D 和 Dense 模型会导致 "RuntimeError: You must compile your model before using it.",尽管已经编译了合并的模型