Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从函数的输出(单数图)创建子图_Python_Matplotlib_Seaborn - Fatal编程技术网

Python 从函数的输出(单数图)创建子图

Python 从函数的输出(单数图)创建子图,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我有一个函数,它返回一个单数图。我想重复此函数三次,并以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

我有一个函数,它返回一个单数图。我想重复此函数三次,并以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)

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

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

#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()
输出:

您有没有看过这本书?