简体   繁体   English

如何在 Plotly Python 中更改散点图?

[英]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 .无论如何,在我看来你想要得到类似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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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