如何在 Python 中使用 Plotly 绘制 bupar 优先矩阵?

How to plot bupar Precedence Matrix using Plotly in Python?

我正在尝试使用 Plotly 绘制一个 bupar Precedence Matrix,如下所示。

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

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

如何根据 bupar Precedence Matrix?

plotly 中添加文本注释和创建矩形

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,然后更新轨迹: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()

编辑:删除参数 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()