简体   繁体   English

修改plotly布局y轴

[英]Modify plotly layout y-axis

I am working on two datasets on churn classification, my problem is as you can see below on the two graph the y-axis are not on the same scale.我正在研究关于流失分类的两个数据集,我的问题是,正如您在下面的两个图表中看到的那样,y 轴的比例不同。 Bank stops at 0.8 and telco-europa at 1, I would like to force the y-axis to always display 0, 0.2, 0.4, 0.6, 0.8, 1.银行停在 0.8,telco-europa 停在 1,我想强制 y 轴始终显示 0、0.2、0.4、0.6、0.8、1。

I have used the following code:我使用了以下代码:

and my histogram is based on this tutorial: https://www.kaggle.com/pavanraj159/telecom-customer-churn-prediction and the bank dataset is this one https://www.kaggle.com/shrutimechlearn/churn-modelling我的直方图基于本教程: https://www.kaggle.com/pavanraj159/telecom-customer-churn-prediction银行数据集是这个https://www.kaggle.com/chrutimechling/

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)

在此处输入图像描述

And here you can fin the dataframe as a dictionary "report_df_scoring" for only the "Bank" dataset在这里,您可以将 dataframe 作为字典“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']"}}

You can access and edit the range of any axis of your figure using:您可以使用以下方法访问和编辑图形任何轴的范围:

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

And set the range like:并将范围设置为:

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

The same thing goes for your tickvals:你的滴答声也是如此:

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

You can use:您可以使用:

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

Your code example does not work for me because "report_df_scoring" is missing.您的代码示例对我不起作用,因为缺少“report_df_scoring”。

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

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