如何在Plotly Python中更改分散的箱形图?

问题描述 投票:0回答:1

我如何更改此情节或某种情节,以便进行性别比较?因为目前您所看到的图片在我的盒子中散布着。

data=pd.DataFrame({"Sex":["male", "male", "male", "female", "female", "female"], "Credit amount":[2000, 3000, 4000, 2500, 3300, 6000]})

我的代码:

df_good = data[data["Sex"] == 'male']
df_bad = data[data["Sex"] == 'female']

trace0 = go.Box(
    y=df_good["Credit amount"],
    x=df_good["Sex"],
    name='Male',
    marker=dict(
        color='#3D9970'
    )
)

trace1 = go.Box(
    y=df_bad['Credit amount'],
    x=df_bad['Sex'],
    name='Female',
    marker=dict(
        color='#FF4136'
    )
)

data = [trace0, trace1]

layout = go.Layout(
    yaxis=dict(
        title='Cheking distribuition'
    ),
    boxmode='group'
)
fig = go.Figure(data=data, layout=layout)

py.iplot(fig, filename='box-age-cat')

enter image description here

python pandas plotly boxplot
1个回答
0
投票

您可以在代码中进行一些改进。与性别相关的不好/不好的名字并不是您最后的选择。无论如何,在我看来,您想要获得distplot之类的东西。对于您的示例,您只需执行以下操作:

import pandas as pd
import plotly.express as px
df = pd.DataFrame({"Sex":["male", "male", "male", "female", "female", "female"],
                   "Credit amount":[2000, 3000, 4000, 2500, 3300, 6000]})

fig = px.box(df,
             x="Sex",
             y="Credit amount",
             color="Sex",
             points="all")
fig.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.