python - Plotly:在热图中穿过单元格中间的形状线

标签 python plotly line 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()

输出如下: enter image description here

我不希望矩形穿过单元格,我想要这样的效果,但它不起作用: enter image description here

最佳答案

您最好的选择似乎是:

<强>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 :

enter image description here

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

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

关于python - Plotly:在热图中穿过单元格中间的形状线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65922115/

相关文章:

python - Pandas 根据列 dtypes 进行应用

python - 控制轴的标题标签颜色

python - scipy.optimize.basinhopping 间隔和接受测试语法

javascript - 当plotlyJS中的数据与其他图表差异很大时,如何打破条形图?

python - 如何在 Plotly 中根据 X、Y、Z 数组绘制 3D 曲面?

html - 水平线上的文字...如何制作线条渐变?

python - python 中的线程锁未按预期工作

python - ModuleNotFoundError : No module named 'plotly.graph_objects'

c# - 如何绘制平滑/圆角/曲线图? (C#)

php - Preg_replace 错误,当使用不同的语言文本时。 (摆脱线条)