首页 > 解决方案 > Plotly Python 图中的错误在哪里?

问题描述

我有如下代码:

goodCredit = data[data["Risk"] == 'good']
badCredit = data[data["Risk"] == 'bad']

bar1SA = go.Bar(
    x = goodCredit["Saving accounts"].value_counts().index.values,
    y = goodCredit["Saving accounts"].value_counts().values,
    name='Good credit')

bar2SA = go.Bar(
    x = badCredit["Saving accounts"].value_counts().index.values,
    y = badCredit["Saving accounts"].value_counts().values,
    name='Bad credit')


bar1CA = go.Box(
    x=goodCredit["Checking account"].value_counts().index.values,
    y=goodCredit["Checking account"].value_counts().values,
    name='Good credit')

bar2CA = go.Box(
    x=badCredit["Checking account"].value_counts().index.values,
    y=badCredit["Checking account"].value_counts().values,
    name='Bad credit')



data = [bar1SA, bar2SA, bar1CA, bar2CA]

fig = tls.make_subplots(rows=1, cols=2,
                          subplot_titles=('Count Saving Accounts','Count Checking account'))

fig.append_trace(bar1SA, 1, 1)
fig.append_trace(bar2SA, 1, 1)

fig.append_trace(bar1CA, 1, 2)
fig.append_trace(bar2CA, 1, 2)


fig['layout'].update(height=700, width=800, title='Saving Accounts and Checking account Exploration based on risk')
py.iplot(fig, filename='SAiCAdistribution')

结果如下:

在此处输入图像描述

代码中的错误在哪里,因为你可以看到第二个情节不好,它应该像第一个情节一样。

标签: pythonpandasplotlyseaborn

解决方案


当您说相似时,我假设您希望它们都是条形图。如果是这样,那么切换

bar1CA = go.Box(
    x=goodCredit["Checking account"].value_counts().index.values,
    y=goodCredit["Checking account"].value_counts().values,
    name='Good credit')

bar2CA = go.Box(
    x=badCredit["Checking account"].value_counts().index.values,
    y=badCredit["Checking account"].value_counts().values,
    name='Bad credit')

bar1CA = go.Bar(
    x=goodCredit["Checking account"].value_counts().index.values,
    y=goodCredit["Checking account"].value_counts().values,
    name='Good credit')

bar2CA = go.Bar(
    x=badCredit["Checking account"].value_counts().index.values,
    y=badCredit["Checking account"].value_counts().values,
    name='Bad credit')

应该做的伎俩。


推荐阅读