我的 Y 轴在使用 Plotly 绘制热力图时没有显示所有日期。

huangapple go评论58阅读模式
英文:

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()

我想要的效果如下图所示:

我的 Y 轴在使用 Plotly 绘制热力图时没有显示所有日期。

我的数据框的一个示例:

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

基于提供的代码的图表如下:

我的 Y 轴在使用 Plotly 绘制热力图时没有显示所有日期。

英文:

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()

我的 Y 轴在使用 Plotly 绘制热力图时没有显示所有日期。

What I want is the following:

我的 Y 轴在使用 Plotly 绘制热力图时没有显示所有日期。

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:
我的 Y 轴在使用 Plotly 绘制热力图时没有显示所有日期。

答案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()

我的 Y 轴在使用 Plotly 绘制热力图时没有显示所有日期。

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()

我的 Y 轴在使用 Plotly 绘制热力图时没有显示所有日期。

英文:

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()

我的 Y 轴在使用 Plotly 绘制热力图时没有显示所有日期。

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()

我的 Y 轴在使用 Plotly 绘制热力图时没有显示所有日期。

huangapple
  • 本文由 发表于 2023年3月9日 22:44:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定