【问题标题】:Create subplots from output (a singular plot) of a function从函数的输出(奇异图)创建子图
【发布时间】:2018-05-27 07:51:33
【问题描述】:

我有一个返回奇异图的函数。我想重复这个函数三次,并以 1x3 格式并排显示 3 个图。我该如何实现这一目标?

def plot_learning_curve(estimator, X, y, ylim=None, cv=None,
                        n_jobs=-1, train_sizes=np.linspace(.1, 1.0, 5)):
    """Generate a simple plot of the test and training learning curve"""
    plt.figure()
    plt.title(str(estimator).split('(')[0]+ " learning curves")
    if ylim is not None:
        plt.ylim(*ylim)
    plt.xlabel("Training examples")
    plt.ylabel("Score")
    train_sizes, train_scores, test_scores = learning_curve(
        estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)
    plt.grid()

    plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
                     train_scores_mean + train_scores_std, alpha=0.1,
                     color="r")
    plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
                     test_scores_mean + test_scores_std, alpha=0.1, color="g")
    plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
             label="Training score")
    plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
             label="Cross-validation score")

    plt.legend(loc="best")
    return plt

我已经尝试过这种方法,但它只是返回一个空的 1x3 网格,在这个空网格下面有图

fig, axes = plt.subplots(nrows = 1, ncols = 3, sharex="all", figsize=(15,5), squeeze=False)

axes[0][0] = plot_learning_curve(tuned_clfs_vert_title2[0][0][1],Xs_train1,Y_train1,cv=skfold)
axes[0][1] = plot_learning_curve(tuned_clfs_vert_title2[0][1][1],Xs_train1,Y_train1,cv=skfold)
axes[0][2] = plot_learning_curve(tuned_clfs_vert_title2[0][2][1],Xs_train1,Y_train1,cv=skfold)

我热衷于将此学习曲线绘图功能用作“模块”。我想另一种方法是在这个函数中编写一个循环。

【问题讨论】:

标签: python matplotlib seaborn


【解决方案1】:

您没有为绘图功能提供轴。我不能使用你的代码,因为它不是Minimal, Complete, and Verifiable example。但这里有一种方法可以满足您的需求:

#plot function with defined axis
def plot_subplot(ax, xdata, ydata, plotnr):
    ax.plot(xdata, ydata)
    ax.set_title("Plot {}".format(plotnr))
    return

#subplot grid 2 x 3 to illustrate the example for more than one row/column
fig, axes = plt.subplots(nrows = 2, ncols = 3, sharex = "all", figsize = (15,5), squeeze=False)
#reproducibility seed
np.random.seed(54321)
#loop over axes, we have to flatten the array "axes", if "fig" contains more than one row
for i, ax in enumerate(axes.flatten()):
    #generate random length for data
    lenx = np.random.randint(5, 30)
    #generate random data, provide information to subplot function
    plot_subplot(ax, np.arange(lenx), np.random.randint(1, 100, lenx), i)

plt.show()

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-24
    • 2017-02-04
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    相关资源
    最近更新 更多