简体   繁体   中英

How can I change my scattered box plot in Plotly Python?

How can I change this plot or kind of plot so as to have comparison of gender? Because currently as you can see on the picture boxes in my box plot are scattered.

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

My code:

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='Checking distribution'
    ),
    boxmode='group'
)
fig = go.Figure(data=data, layout=layout)

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

在此处输入图片说明

There are several things you could improve in your code. Not last your choice of names bad/good related to sex. Anyway it looks to me that you want to get something like distplot . Regarding your example you could simply do this:

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM