修改绘图布局y轴

时间:2019-11-14 09:41:28

标签: python plotly

我正在研究流失分类的两个数据集,我的问题是,正如您在下面的两个图上看到的那样,y轴的比例不同。银行在0.8止损,而telco-europa在1止损,我想强制y轴始终显示0、0.2、0.4、0.6、0.8、1。

我使用了以下代码:

我的直方图基于本教程:https://www.kaggle.com/pavanraj159/telecom-customer-churn-prediction,银行数据集就是这个https://www.kaggle.com/shrutimechlearn/churn-modelling

import plotly.graph_objs as go#visualization
import plotly.offline as py#visualization

def output_tracer(metric,color, model_performances) :
    tracer = go.Bar(x = model_performances["Algorithm"] ,
                    y = model_performances[metric],
                    orientation = "v",name = metric ,
                    marker = dict(line = dict(width =.7),
                                  color = color)
                   )
    return tracer

def output_data(model_performances):
    trace1  = output_tracer("1-Precision","#6699FF", model_performances)
    trace2  = output_tracer('1-Recall',"red", model_performances)
    trace3  = output_tracer('1-F1-score',"#33CC99", model_performances)
    trace4  = output_tracer('Accuracy',"lightgrey", model_performances)
    trace5  = output_tracer('AUC',"#FFCC99", model_performances)

    data = [trace1,trace2,trace3,trace4,trace5]    
    return data

def output_layout(model):
    layout = go.Layout(dict(title = model,
                            plot_bgcolor  = "rgb(243,243,243)",
                            paper_bgcolor = "rgb(243,243,243)",
                            xaxis = dict(gridcolor = 'rgb(255, 255, 255)',
                                         title = "",
                                         zerolinewidth=1,
                                         ticklen=5,gridwidth=2),
                            yaxis = dict(gridcolor = 'rgb(255, 255, 255)',
                                         zerolinewidth=1,ticklen=5,gridwidth=2),
                            margin = dict(l = 250),
                            height = 400
                           )
                      )
    return layout
model = "Bank"

model_performances = report_df_scoring[report_df_scoring.Dataset == model]

fig = go.Figure(data=output_data(model_performances),layout=output_layout(model))
py.iplot(fig)

enter image description here

在这里,您可以将数据框仅作为“银行”数据集的字典“ report_df_scoring”来查找

{'Dataset': {0: 'Bank',
  1: 'Bank',
  2: 'Bank',
  3: 'Bank',
  4: 'Bank',
  5: 'Bank',
  6: 'Bank'},
 'Algorithm': {0: 'LogisticRegressionNoSMOTE',
  1: 'Logistic Regression',
  2: 'SVM-linear',
  3: 'SVM-rbf',
  4: 'xgboost',
  5: 'GaussianNB',
  6: 'RandomForest'},
 'W-Precision': {0: 0.8159638339642141,
  1: 0.8229500536388679,
  2: 0.8243426658647828,
  3: 0.7956512785333915,
  4: 0.8288351219512194,
  5: 0.8302513223140496,
  6: 0.8307514249037228},
 'W-Recall': {0: 0.8324,
  1: 0.7636,
  2: 0.7628,
  3: 0.8056,
  4: 0.836,
  5: 0.8176,
  6: 0.8408},
 'W-F1-score': {0: 0.810103868755423,
  1: 0.7811452562742854,
  2: 0.7807117770916884,
  3: 0.7997335148514852,
  4: 0.831622605929424,
  5: 0.7598757585104978,
  6: 0.8336474053248425},
 '0-Precision': {0: 0.8493518104604381,
  1: 0.9187236604455148,
  2: 0.9206541490006056,
  3: 0.8634596695821186,
  4: 0.8834146341463415,
  5: 0.8152892561983471,
  6: 0.8789473684210526},
 '0-Recall': {0: 0.958627648839556,
  1: 0.7699293642785066,
  2: 0.7669021190716448,
  3: 0.8965691220988901,
  4: 0.9137235116044399,
  5: 0.9954591321897074,
  6: 0.9268415741675076},
 '0-F1-score': {0: 0.9006873666745674,
  1: 0.8377710678012626,
  2: 0.8367740159647675,
  3: 0.8797029702970298,
  4: 0.8983134920634921,
  5: 0.8964107223989097,
  6: 0.9022593320235756},
 '1-Precision': {0: 0.6882129277566539,
  1: 0.4564958283671037,
  2: 0.4558303886925795,
  3: 0.5361990950226244,
  4: 0.62,
  5: 0.8875,
  6: 0.6463414634146342},
 '1-Recall': {0: 0.34942084942084944,
  1: 0.7393822393822393,
  2: 0.747104247104247,
  3: 0.4575289575289575,
  4: 0.5386100386100386,
  5: 0.13706563706563707,
  6: 0.5115830115830116},
 '1-F1-score': {0: 0.4635083226632522,
  1: 0.5644804716285925,
  2: 0.5662033650329188,
  3: 0.49375,
  4: 0.5764462809917356,
  5: 0.2374581939799331,
  6: 0.5711206896551725},
 'Accuracy': {0: 0.8324,
  1: 0.7636,
  2: 0.7628,
  3: 0.8056,
  4: 0.836,
  5: 0.8176,
  6: 0.8408},
 'AUC': {0: 0.6540242491302027,
  1: 0.754655801830373,
  2: 0.7570031830879459,
  3: 0.6770490398139237,
  4: 0.7261667751072393,
  5: 0.5662623846276723,
  6: 0.7192122928752596},
 'SMOTE': {0: 'No',
  1: 'Yes',
  2: 'Yes',
  3: 'Yes',
  4: 'Yes',
  5: 'Yes',
  6: 'Yes'},
 'top3var': {0: "['numofproducts_4', 'numofproducts_3', 'geography_germany']",
  1: "['numofproducts_4', 'numofproducts_3', 'geography_germany']",
  2: "['numofproducts_4', 'numofproducts_3', 'age']",
  3: "['empty']",
  4: "['numofproducts_2', 'numofproducts_1', 'isactivemember']",
  5: "['empty']",
  6: "['age', 'numofproducts_2', 'balance']"}}

2 个答案:

答案 0 :(得分:1)

您可以使用以下方法访问和编辑图形中任意轴的范围:

fig['layout']['yaxis']['range']

然后将范围设置为:

fig['layout']['yaxis']['range'] = [0, 1]

同样的事情适用于您的滴答声:

fig['layout']['yaxis']['tickvals'] = [0, 0.2, 0.4, 0.6, 0.8, 1]

答案 1 :(得分:0)

您可以使用:

fig.update_yaxes(tickvals=[0, 0.2, 0.4, 0.6, 0.8, 1])

您的代码示例对我不起作用,因为缺少“ report_df_scoring”。