Plotly:通过热图中单元格中间的形状线

Plotly: Shape lines passing in the middle of the cells in a heatmap

我正在使用 python 中的 plotly 绘制热图。我想在某些区域周围绘制一个矩形,我按如下方式进行:

import plotly.graph_objs as go
import plotly.figure_factory as ff

layout_heatmap = go.Layout(
        xaxis=dict(title='Years'),
        yaxis=dict(title='Years'),
    )

ff_fig = ff.create_annotated_heatmap(x=all_years, y=all_years, z=heatmap, showscale=True,
                                         colorscale='Viridis',)
fig = go.FigureWidget(ff_fig)
fig.layout = layout_heatmap
fig.layout.annotations = ff_fig.layout.annotations
fig['layout']['yaxis']['autorange'] = "reversed"
  
fig.add_shape(type="rect",
              x0=1960, y0=1960, x1=1966, y1=1966,
              line=dict(color="red"),
              )
fig.add_shape(type="rect",
              x0=1967, y0=1967, x1=1970, y1=1970,
              line=dict(color="red"),
              )
fig.show()

输出如下:

我不想让矩形穿过单元格,我想要这样的效果,但它不起作用:

您最好的选择似乎是:

1.将轴类型更改为分类:

fig.update_xaxes(type='category')
fig.update_yaxes(type='category')

2。按位置而不是值放置形状:

fig.add_shape(type="rect",
              x0=-0.5, y0=1.5, x1=3.5, y1=5.5,
              line=dict(color="blue", width = 4),
              )

fig.add_shape(type="rect",
              x0=3.5, y0=-0.5, x1=5.5, y1=1.5,
              line=dict(color="green", width = 4),
              )

剧情:

带有内置 plotly 数据集的完整代码

import plotly.express as px
import plotly.figure_factory as ff
import pandas as pd

df = px.data.stocks()#.tail(50)
df = df.drop(['date'], axis = 1)
dfc = df.corr()
z = dfc.values.tolist()

# change each element of z to type string for annotations
# z_text = [[str(y) for y in x] for x in z]
z_text = [[str(round(y, 1)) for y in x] for x in z]
# df.columns =['2014', '2015', '2016', '2017', '2018', '2019']
df.columns =[1960, 1962, 1964, 1966, 1968, 1970]
# df.columns =['y1960', 'y1962', 'y1964', 'y1966', 'y1968', 'y1970']

# set up figure 
fig = ff.create_annotated_heatmap(z, x=list(df.columns),
                                     y=list(df.columns),
                                     annotation_text=z_text, colorscale='agsunset')

# add custom xaxis title
fig.add_annotation(dict(font=dict(color="black",size=14),
                        x=0.5,
                        y=-0.15,
                        showarrow=False,
                        text="",
                        xref="paper",
                        yref="paper"))

# add custom yaxis title
fig.add_annotation(dict(font=dict(color="black",size=14),
                        x=-0.35,
                        y=0.5,
                        showarrow=False,
                        text="",
                        textangle=-90,
                        xref="paper",
                        yref="paper"))

fig.add_shape(type="rect",
              x0=-0.5, y0=1.5, x1=3.5, y1=5.5,
              line=dict(color="blue", width = 4),
              )

fig.add_shape(type="rect",
              x0=3.5, y0=-0.5, x1=5.5, y1=1.5,
              line=dict(color="green", width = 4),
              )


# adjust margins to make room for yaxis title
fig.update_layout(margin=dict(t=50, l=200))

# add colorbar
fig['data'][0]['showscale'] = True
fig.update_xaxes(type='category')
fig.update_yaxes(type='category')
fig.show()