为什么用 Plotly 显示热图时会出现这个问题?

Why this problem when showing the heatmap with Plotly?

我想用 plotly 创建一个简单的带注释的热图。 但是当 y 值包含年份 ('2017','2018','2019')

例如:

import plotly.figure_factory as ff

# create a x list (month)
x = ['January',
     'February',
     'March']

#create a y list (year)
y=['2017', '2018', '2019', 'Mean']

#create a z list (the values)
z = [[1,2,3],
    [4,5,6],
    [7,8,9],
    [4,5,6]]

fig = ff.create_annotated_heatmap(z,x=x,y=y)
fig.show()

上面的代码给出了这个结果:

但是,如果我用其他字符串更改 y 列表。没关系,我得到了我想要的。

import plotly.figure_factory as ff

# create a x list (month)
x = ['January',
     'February',
     'March']

#create a y list (year)
y=['year 1', 'year 2', 'year 3', 'Mean']

#create a z list (the values)
z = [[1,2,3],
    [4,5,6],
    [7,8,9],
    [4,5,6]]

fig = ff.create_annotated_heatmap(z,x=x,y=y)
fig.show()

我该如何解决这个问题?

尝试添加 fig.update_layout(yaxis=dict(type='category'))。这应确保将 y 轴上的值解释为字符串。

import plotly.figure_factory as ff

# create a x list (month)
x = ['January',
     'February',
     'March']

#create a y list (year)
y=['2017', '2018', '2019', 'Mean']

#create a z list (the values)
z = [[1,2,3],
    [4,5,6],
    [7,8,9],
    [4,5,6]]

fig = ff.create_annotated_heatmap(z,x=x,y=y)

fig.update_layout(yaxis=dict(type='category'))

fig.show()