【问题标题】:How to combine 2 plots in 1 cell, Python?如何在 1 个单元格中合并 2 个图,Python?
【发布时间】:2023-03-02 22:51:01
【问题描述】:

您能否以这种方式更改我的代码,以便将这 2 个图放在旁边,意思是 1 行和 2 列(子图 nrows=1,ncols=2)?目前我将这些图表放在 2 个单独的单元格中,我希望将它们放在 1 个单元格中。

我的代码: 第一个情节:

from yellowbrick.classifier import (PrecisionRecallCurve)
fig, ax = plt.subplots(figsize=(10, 6))
viz = PrecisionRecallCurve(DecisionTreeClassifier(max_depth=4))
viz.fit(X_train_model_2, y_train_model_2)
print(viz.score(X_test_model_2, y_test_model_2))
viz.ax.set(title="Krzywa precyzja-czułość klasyfikatora drzewa losowego",
           xlabel="Czułość",
           ylabel="Precyzja")
ax.legend(("Binarna krzywa precyzja-czułość",
           "Średnia precyzja = {:0.2f}".format(viz.score(X_test_model_2,y_test_model_2))),
          frameon=True,
          loc="lower left")

plt.show()

第二个情节:

import scikitplot as skplt
fig, ax = plt.subplots(figsize=(10, 6))
y_probas = decision_tree.predict_proba(X_test_model_2)
skplt.metrics.plot_cumulative_gain(y_test_model_2,
                                   y_probas,
                                   ax=ax)
ax.set(title="Krzywa skumulowanych zysków",
       xlabel="Odsetek próbek",
       ylabel="Zysk")
ax.legend(("Klasa 0",
           "Klasa 1",
           "Krzywa odniesienia"),
          frameon=True,
          loc="lower right")
plt.show()

【问题讨论】:

    标签: python matplotlib yellowbrick


    【解决方案1】:

    也许有帮助:

    from yellowbrick.classifier.prcurve import PrecisionRecallCurve                                                                                                                                          
    import scikitplot as skplt
    import numpy as np
    import sklearn
    import sklearn.tree
    import matplotlib.pyplot as plt
    
    #generate some test data
    X = np.arange(200)+np.random.normal(0,10,200)
    y = np.array([True if (x <100) and (x > 50) else False for x in X])
    X = X.reshape(-1,1)
    
    X_train_model_2 = []
    y_train_model_2 = []
    X_test_model_2 = []
    y_test_model_2 = []
    
    X_train_model_2,X_test_model_2,y_train_model_2,y_test_model_2=
    sklearn.model_selection.train_test_split(
                                             X, y,
                                             test_size=0.4,
                                             random_state=0)
    
    fig, (ax1, ax2) = plt.subplots(1,2)  #1 row, 2 columns
    
    viz = PrecisionRecallCurve(sklearn.tree.DecisionTreeClassifier(max_depth=4),
          ax = ax1) #set the axis to plot one (ax1)
    decision_tree = viz.fit(X_train_model_2, y_train_model_2)
    print(viz.score(X_test_model_2, y_test_model_2))
    
    #Set the attributes for plot one
    ax1.set(title="Krzywa precyzja-czułość klasyfikatora drzewa losowego",
            xlabel="Czułość",
            ylabel="Precyzja")
    ax1.legend(("Binarna krzywa precyzja-czułość",
            "Średnia precyzja  {:0.2f}".format(viz.score(X_test_model_2,y_test_model_2))),
            frameon=True,
            loc="lower left")
    y_probas = decision_tree.predict_proba(X_test_model_2)
    
    skplt.metrics.plot_cumulative_gain(y_test_model_2,
                                       y_probas,
                                       ax=ax2) #set the axis to plot two (ax2)
    
    #Set the attributes for plot two
    ax2.set(title="Krzywa skumulowanych zysków",
            xlabel="Odsetek próbek",
            ylabel="Zysk")
    ax2.legend(("Klasa 0",
                "Klasa 1",
                "Krzywa odniesienia"),
                frameon=True,
                loc="lower right")
    #Show the whole plot
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      相关资源
      最近更新 更多