plotly热图不能颠倒ytick顺序

问题描述 投票:0回答:2

[我曾经使用seaborn来创建热图,但是我爱上了plotly,并且自从我试图将所有可视化更改为plotly以来。

我已经成功创建了热图,但是即使尝试了两种方法,创建热图时yaxis的顺序也不会改变。

这里是MRE:

import plotly.graph_objects as go

y = ["a", "b", "c", "d", "e"]
new_y = ["e", "d", "c", "b","a"]

data = [
    go.Heatmap(
        z=[[6,5,4,3,1], [5,4,3,2,np.nan], [6,4,3,np.nan, np.nan], [5,3,np.nan,np.nan,np.nan],[4,np.nan,np.nan,np.nan,np.nan]],
        x=[1,2,3,4,5],
        y=new_y,
        colorscale="YlOrRd"
    )
]

layout = go.Layout(
    title = "title"
)

fig = go.Figure(data=data, layout=layout)
fig.show()

插入y变量的列表无关紧要。 y或y_new它给了我相同的热图。enter image description here

如何反转yaxis,使“ e”位于顶部?

[如果有人知道他们在seaborn默认热图中使用的色标名称,请告诉我,因为我喜欢这种颜色。

提前感谢。

python plotly heatmap
2个回答
1
投票

要在布局中反转y轴,请添加以下设置。

layout = go.Layout(
    title = "title",
    yaxis=dict(visible=True,autorange='reversed')
)

1
投票

由于y轴是分类的,因此应在布局中包括yaxis_type = "category"。内置的色标在以下链接中列出:https://plotly.com/python/builtin-colorscales/

import plotly.graph_objects as go
import numpy as np

data = go.Heatmap(
        z=[[6, 5, 4, 3, 1], [5, 4, 3, 2, np.nan], [6, 4, 3, np.nan, np.nan], [5, 3, np.nan, np.nan, np.nan],[4, np.nan, np.nan, np.nan, np.nan]],
        x=[1, 2, 3, 4, 5],
        y=["a", "b", "c", "d", "e"],
        colorscale="Inferno"
)

layout = go.Layout(yaxis_type="category")

fig = go.Figure(data=data, layout=layout)

fig.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.