我试图用Plotly绘制一个bupar Precedence Matrix,如下所示.

enter image description here

到目前为止,我试图绘制如下散点图:

import plotly.express as px

fig = px.scatter(df, 
                 y="Antecedent", 
                 x="Consequent", 
                 size="Absolute_Frequency", 
                 color="Absolute_Frequency")

fig.update_traces(
    hovertemplate="<br>".join([
        "Antecedent: %{y}",
        "Consequent: %{x}"
    ]),
    name=""
)

fig.update_layout(
    template="plotly_white",
    # Figure title styling
    title= dict(
        text="Precedence Matrix",
        x=0.5,
        y=0.95,
        font=dict(
            family="Rockwell",
            size=20,
            color='#000000'
            )
        ),
    xaxis_title="Consequent",
    yaxis_title="Antecedent",
    font=dict(
        family="Rockwell",
        size=16),
    # Fig height
    height=600, 
    width=1200,
    # Turn off legend
    showlegend=False,
    hoverlabel=dict(
        bgcolor="white",
        font_size=16,
        font_family="Rockwell"
        )
    )

fig.show()

如何在plotly中添加文本注释并按照bupar Precedence Matrix创建矩形?

enter image description here

Data Prep

Antecedent Consequent Absolute_Frequency
register request examine thoroughly 1
examine thoroughly check ticket 2
check ticket decide 6
decide reject request 3
register request check ticket 2
check ticket examine casually 2
examine casually decide 2
decide pay compensation 3
register request examine casually 3
examine casually check ticket 4
decide reinitiate request 3
reinitiate request examine thoroughly 1
check ticket examine thoroughly 1
examine thoroughly decide 1
reinitiate request check ticket 1
reinitiate request examine casually 1

推荐答案

你很接近了:如果你将参数text="Absolute_Frequency"添加到px.scatter,然后更新traces:fig.update_traces(marker={'symbol': 'square'}, textfont_color='white', textposition='middle center'),这将有望为你提供所需的图表.

注释:我注意到在原始图表中,标记的大小都相同.在您包含的代码中,标记的大小取决于其绝对频率,这意味着文本有时比标记大.如果希望标记的大小都相同,可以删除参数size="Absolute_Frequency"

fig = px.scatter(df, 
                 y="Antecedent", 
                 x="Consequent", 
                 size="Absolute_Frequency", 
                 color="Absolute_Frequency",
                 text="Absolute_Frequency"
                 )

fig.update_traces(
    hovertemplate="<br>".join([
        "Antecedent: %{y}",
        "Consequent: %{x}"
    ]),
    name=""
)

fig.update_layout(
    template="plotly_white",
    # Figure title styling
    title= dict(
        text="Precedence Matrix",
        x=0.5,
        y=0.95,
        font=dict(
            family="Rockwell",
            size=20,
            color='#000000'
            )
        ),
    xaxis_title="Consequent",
    yaxis_title="Antecedent",
    font=dict(
        family="Rockwell",
        size=16),
    # Fig height
    height=600, 
    width=1200,
    # Turn off legend
    showlegend=False,
    hoverlabel=dict(
        bgcolor="white",
        font_size=16,
        font_family="Rockwell"
        )
    )

fig.update_traces(marker={'symbol': 'square'}, textfont_color='white', textposition='middle center')
fig.show()

enter image description here

编辑:删除参数size="Absolute_Frequency"后,可以更改标记大小并设置字体大小,使其更接近最初显示的图像.

fig = px.scatter(df, 
                 y="Antecedent", 
                 x="Consequent", 
                 # size="Absolute_Frequency", 
                 color="Absolute_Frequency",
                 text="Absolute_Frequency"
                 )

fig.update_traces(
    hovertemplate="<br>".join([
        "Antecedent: %{y}",
        "Consequent: %{x}"
    ]),
    name=""
)

fig.update_layout(
    template="plotly_white",
    # Figure title styling
    title= dict(
        text="Precedence Matrix",
        x=0.5,
        y=0.95,
        font=dict(
            family="Rockwell",
            size=20,
            color='#000000'
            )
        ),
    xaxis_title="Consequent",
    yaxis_title="Antecedent",
    font=dict(
        family="Rockwell",
        size=16),
    # Fig height
    height=600, 
    width=1200,
    # Turn off legend
    showlegend=False,
    hoverlabel=dict(
        bgcolor="white",
        font_size=16,
        font_family="Rockwell"
        )
    )

fig.update_traces(
    marker={'symbol': 'square', 'size': 50}, 
    textfont_color='white', 
    textfont_size=14,
    textposition='middle center'
)
fig.show()

enter image description here

Python相关问答推荐

Flask:如何在完整路由代码执行之前返回验证

Altair -箱形图边界设置为黑色,中线设置为红色

使用多个性能指标执行循环特征消除

在上下文管理器中更改异常类型

有什么方法可以避免使用许多if陈述

剧作家Python:expect(locator).to_be_visible()vs locator.wait_for()

理解Python的二分库:澄清bisect_left的使用

如何自动抓取以下CSV

pandas DataFrame GroupBy.diff函数的意外输出

处理(潜在)不断增长的任务队列的并行/并行方法

加速Python循环

如何更改分组条形图中条形图的 colored颜色 ?

cv2.matchTemplate函数匹配失败

如何使用scipy的curve_fit与约束,其中拟合的曲线总是在观测值之下?

Python Pandas获取层次路径直到顶层管理

需要帮助重新调整python fill_between与数据点

使用Python和文件进行模糊输出

使用Python从rotowire中抓取MLB每日阵容

如何杀死一个进程,我的Python可执行文件以sudo启动?

在pandas/python中计数嵌套类别