简体   繁体   English

Plotly Python:带多个子图的降序排序条

[英]Plotly Python: sort bar descending with multiple subplots

I have a 4 chart subplot in Python that I am using to help calculate model selection.我在 Python 中有一个 4 图表子图,用于帮助计算 model 选择。 The models are regression models, so I am using a mix of histograms (predictions x actuals) and bar charts (train, test, cv scores).这些模型是回归模型,所以我混合使用了直方图(预测 x 实际值)和条形图(训练、测试、CV 分数)。 My code is as follows:我的代码如下:

fig = make_subplots(3,2, specs=[[{'colspan':2}, None],
                                 [{'colspan':2}, None],
                                [{'type':'xy'}, {'type':'xy'}]
                               ],
                   subplot_titles=('Log of Predictions and Actuals','Test and Train Scores','Test Score','Cross Validation'))

fig.add_histogram(x=np.log(y_test), name='Actuals', xbins={'size':0.1},
                 row=1,col=1)
fig.add_histogram(x=np.log(preds), name='Predictions', xbins={'size':0.1},
                 row=1,col=1),

for score in ['test','train']:
    fig.add_bar(x=scores_kf_df.T.index,y=scores_kf_df.T[str(score)], name=score, row=2, col=1)
    
for score in ['test']:
    fig.add_bar(x=scores_kf_df.T.index,y=scores_kf_df.T[str(score)], name=score, row=3, col=1)

for score in ['cv']:
    fig.add_bar(x=scores_kf_df.T.index,y=scores_kf_df.T[str(score)], name=score, row=3, col=2)

fig.update_layout({'height':1200,'width':800,
                  'title':{'text':'Accuracy Metrics of Each Model','x':0.5, 'font':{'size':28}},
                  'xaxis':{'categoryorder':'total descending'}})

My output is as follows:我的output如下:绘制子图

My question is, how do I make the bottom three bar charts so that they are in line the way bar charts should be?我的问题是,我如何制作底部的三个条形图,以便它们与条形图应该是一致的? I would like to sort by descending for each of these, but the only thing I can find is fig.update_layout({'xaxis':'total descending'}), which doesn't work.我想对其中的每一个进行降序排序,但我唯一能找到的是 fig.update_layout({'xaxis':'total descending'}),它不起作用。

How do I sort by descending when it comes to multiple subplots?当涉及到多个子图时,如何按降序排序?

Figured it out - simple solution of reordering the dataframe prior to plotting.想通了 - 在绘图之前重新排序 dataframe 的简单解决方案。

for score in scores_kf_df.T.columns:
    df_sorted = scores_kf_df.T.sort_values(by=score, ascending=False)
    fig.add_bar(x=df_sorted.index,y=df_sorted[str(score)], name=score, row=1, col=1)

df_sorted here is just all values sorted by descending.这里的 df_sorted 只是所有按降序排序的值。

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

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