Matplotlib 将打印对象添加到轴中的图形

Matplotlib 将打印对象添加到轴中的图形,matplotlib,Matplotlib,我试图在图形中添加一个绘图,类似这样: fig, axs = plt.subplots(1,2, figsize =(10,5)) plot1 = customized_function(x1, y1) # any plot object plot2 = customized_function(x2, y2) # any plot object axs[0] = plot1 # adding the plot1 to the figure axs[1] = plot2 # adding the

我试图在图形中添加一个绘图,类似这样:

fig, axs = plt.subplots(1,2, figsize =(10,5))
plot1 = customized_function(x1, y1) # any plot object
plot2 = customized_function(x2, y2) # any plot object
axs[0] = plot1 # adding the plot1 to the figure
axs[1] = plot2 # adding the plot2 to the figure
但是我找不到方法将
plot1
plot2
添加到图形中。我一直在到处寻找解决方案,但该解决方案不适合我的需要。我找到的解决办法是:

fig, (ax1, ax2) = plt.subplots(2)
fig.suptitle('Axes values are scaled individually by default')
ax1.plot(x, y)
ax2.plot(x + 1, -y)
但是,我想添加的绘图已经完成

有什么建议吗

可复制代码:

from sklearn.metrics import roc_curve
def customized_function(y_train, prob_train):
    fpr = dict()
    tpr = dict()

    fpr, tpr, _ = roc_curve(y_train, prob_train)
    roc_auc = dict()
    roc_auc = auc(fpr, tpr)

    # make the plot
    plt.figure(figsize=(10, 10))
    plt.plot(fpr, tpr)
    plt.title('ROC curve and AUC')
    plt.show()
    

y_train = np.array([0,0,0,0,0,1,1,1,0,1])
prob_train = np.array([0.1,0.2,0.3,0.4,0.5,0.1,0.1,0.8,0.9,1])

y_test = np.array([0,1,1,0,0,0,1,0,0,1])
prob_test = np.array([0.1,0.4,0.2,0.5,0.1,0.1,0.2,0.8,0.3,0.1])

customized_function(y_train, prob_train)
customized_function(y_test, prob_test)

编写绘图函数的推荐方法是将轴的引用传递给函数:

def function_customized_plot(x, y, ax=None):
    if ax is None:
        ax = plt.gca()
    (...) # rest of your code here

fig, axs = plt.subplots(1,2, figsize =(10,5))
function_customized_plot(x1, y1, ax=axs[0])
function_customized_plot(x2, y2n ax=axs[1])

我真的听不懂你的意思。在
ax2.plot(x+1,-y)
之后,您仍然可以自定义打印样式,如字体大小、记号、标签、文本等。上面的第一个代码块不是在matplotlib中打印的标准方式。它运行,但不起作用。它仍然返回一个带有两个空绘图的图形。由于您没有提供绘图函数的代码,因此我无法再进一步,我向您道歉,因为我认为这很简单。我添加了一个可复制的代码。