如何在 Python Plotly 中使用 `px.imshow` 热力图中使散点图抖动?

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

how to jitter the scatter plot on px.imshow heatmap in python plotly

问题

我使用plotly的热力图制作了一个风险矩阵,并在热力图上叠加了散点以显示每个单独的项目。我的问题是,如果我有2个项目位于同一个框中,如何使这些点稍微偏移,以便它们都可以适应框内,而不会重叠在一起?例如,我在影响中等,可能性低的框中有"project1"。如果我有一个"project2"也位于同一个框中,如何使它们都适应同一个框内而不重叠?

如何在 Python Plotly 中使用 `px.imshow` 热力图中使散点图抖动?

import plotly.express as px
import plotly.graph_objects as go

fig = px.imshow([[3, 5, 5],
                 [1, 3, 5],
                 [1, 1, 3]],
                color_continuous_scale='Reds',
                labels=dict(x="Likelihood", y="Impact"),
                x=['Low', 'Medium', 'High'],
                y=['High', 'Medium', 'Low']
                )
fig.add_trace(go.Scatter(x=["Low"], y=["Medium"],
                         name="project1",
                         marker=dict(color='black', size=16)))
fig.show()

期望的结果

如何在 Python Plotly 中使用 `px.imshow` 热力图中使散点图抖动?

英文:

I have a risk matrix made using plotly's heatmap, and I have overlayed scatter points on the heatmap to show as each individual project. My question is if I have 2 projects that are in the same box, how to jitter the points so they can both fit in the box without overlaying onto each other? For example, I have project1 in the box of impact medium, likelihood low. If I have a project2 that's in the same box, how can both fit in the same box without overlaying?

如何在 Python Plotly 中使用 `px.imshow` 热力图中使散点图抖动?

import plotly.express as px
import plotly.graph_objects as go

fig = px.imshow([[3, 5, 5],
                 [1, 3, 5],
                 [1, 1, 3]],
                color_continuous_scale='Reds',
                labels=dict(x="Likelihood", y="Impact"),
                x=['Low', 'Medium', 'High'],
                y=['High', 'Medium', 'Low']
                )
fig.add_trace(go.Scatter(x=["Low"], y=["Medium"],
                         name="project1",
                         marker=dict(color='black', size=16)))
fig.show()

Expected Outcome

如何在 Python Plotly 中使用 `px.imshow` 热力图中使散点图抖动?

答案1

得分: 1

使用一个图形对象,创建一个以xy轴为数值轴的图形,然后将抖动值设置为xy轴数值的散点图。最后,可以通过将xy轴的刻度更新为字符串,将xy轴呈现为一个分类变量。我们猜测并手动添加您期望图形的抖动,然后用您的数据替换它。

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Heatmap(z=[[3, 5, 5],
                            [1, 3, 5],
                            [1, 1, 3]],
                         colorscale='Reds'))
fig.add_trace(go.Scatter(x=[-0.25], y=[0.75], name="project1", marker=dict(color='black', size=16), showlegend=False))
fig.add_trace(go.Scatter(x=[-0.25], y=[1.25], name="project1", marker=dict(color='black', size=16), showlegend=False))
fig add_trace(go.Scatter(x=[0.25], y=[0.75], name="project1", marker=dict(color='black', size=16), showlegend=False))
fig.add_trace(go.Scatter(x=[0.25], y=[1.25], name="project1", marker=dict(color='black', size=16), showlegend=False))
fig.add_trace(go.Scatter(x=[0], y=[1.0], name="project1", marker=dict(color='black', size=8), showlegend=False))

fig.update_layout(height=400, width=400)
fig.update_xaxes(tickvals=[0,1,2], ticktext=['Low', 'Medium', 'High'])
fig.update_yaxes(tickvals=[0,1,2], ticktext=['High', 'Medium', 'Low'], autorange='reversed')
fig.show()

如何在 Python Plotly 中使用 `px.imshow` 热力图中使散点图抖动?

英文:

Using a graph object, create a graph with the xy-axis as the numeric axis, and then set the jitter value as a scatter with the xy-axis values. Finally, the xy-axis can be presented as a categorical variable by updating the xy-axis scale with a string. We take a guess and read the jitter of your expected graph and add it manually. Replace it with your data.

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Heatmap(z=[[3, 5, 5],
                            [1, 3, 5],
                            [1, 1, 3]],
                         colorscale='Reds'))
fig.add_trace(go.Scatter(x=[-0.25], y=[0.75], name="project1", marker=dict(color='black', size=16), showlegend=False))
fig.add_trace(go.Scatter(x=[-0.25], y=[1.25], name="project1", marker=dict(color='black', size=16), showlegend=False))
fig.add_trace(go.Scatter(x=[0.25], y=[0.75], name="project1", marker=dict(color='black', size=16), showlegend=False))
fig.add_trace(go.Scatter(x=[0.25], y=[1.25], name="project1", marker=dict(color='black', size=16), showlegend=False))
fig.add_trace(go.Scatter(x=[0], y=[1.0], name="project1", marker=dict(color='black', size=8), showlegend=False))

fig.update_layout(height=400, width=400)
fig.update_xaxes(tickvals=[0,1,2], ticktext=['Low', 'Medium', 'High'])
fig.update_yaxes(tickvals=[0,1,2], ticktext=['High', 'Medium', 'Low'], autorange='reversed')
fig.show()

如何在 Python Plotly 中使用 `px.imshow` 热力图中使散点图抖动?

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

发表评论

匿名网友

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

确定