【问题标题】:How to get matplotlib ax object as output of a function, and use it for plotting?如何获取 matplotlib ax 对象作为函数的输出,并将其用于绘图?
【发布时间】:2021-12-03 14:27:56
【问题描述】:

我想在一张图中画好几张图。

为了绘制一组数据,我定义了一个返回 ax 的函数:

def make_plot(y_true, y_pred, plot_size) :
    fig = plt.figure(figsize=(plot_size))
    ax = fig.add_subplot(1,1,1)
    ax.plot(y_true, y_pred, 'o')
    ax.set_xlabel('Observed', size=14)
    ax.set_ylabel('Predicted', size=14)
    ax.tick_params(labelsize=12)
    return ax

然后,我对数据有一个 For 循环,为每组数据绘制一个图,并将所有图组合在一个图中。以下是代码:

import matplotlib.pyplot as plt
import math


def make_plot(y_true, y_pred, plot_size) :
    fig = plt.figure(figsize=(plot_size))
    ax = fig.add_subplot(1,1,1)
    ax.plot(y_true, y_pred, 'o')
    ax.set_xlabel('Observed', size=14)
    ax.set_ylabel('Predicted', size=14)
    ax.tick_params(labelsize=12)
    return ax
    

def plot_all(y_true_all, y_pred_all, fig_save_folder, fig_name, plot_size=(4,4), num_plots_x = 2):

    num_plots_y = math.ceil(len(y_true_all)/num_plots_x)   # No. of plots in y direction
    
    plt.figure(figsize=(plot_size[0]*num_plots_x, plot_size[1]*num_plots_y))
    
    for i in range(len((y_true_all))):
        ax = plt.subplot(num_plots_y, num_plots_x, i+1)   # [row, column]        
        
        y_true = y_true_all[i]
        y_pred = y_pred_all[i]
    
        ax = make_plot(y_true, y_pred, plot_size) 
    
    plt.tight_layout()     
    plt.savefig(f'{fig_save_folder}/{fig_name}.png')
    plt.show()   
    
    
y_true_all = [[1, 2, 3], [1, 2, 3]]
y_pred_all = [[1.1, 2, 3.1], [1, 1.9, 3]]

fig_save_folder = './result'
fig_name = 'test'
plot_all(y_true_all, y_pred_all, fig_save_folder, fig_name, plot_size=(4,4), num_plots_x = 2)

我想要得到的是如下图:

但是,我得到了两个空轴和一个绘图。如果您有任何解决此问题的想法,请告诉我。

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    将您的函数传递给Axes 更简单更好:

    import matplotlib.pyplot as plt
    import math
    
    
    def make_plot(y_true, y_pred, ax) :
        ax.plot(y_true, y_pred, 'o')
        ax.set_xlabel('Observed', size=14)
        ax.set_ylabel('Predicted', size=14)
        ax.tick_params(labelsize=12)
        return ax
        
    
    def plot_all(y_true_all, y_pred_all, fig_save_folder, fig_name, plot_size=(4,4), num_plots_x = 2):
    
        num_plots_y = math.ceil(len(y_true_all)/num_plots_x)   # No. of plots in y direction
        
        plt.figure(figsize=(plot_size[0]*num_plots_x, plot_size[1]*num_plots_y))
        
        for i in range(len((y_true_all))):
            ax = plt.subplot(num_plots_y, num_plots_x, i+1)   # [row, column]        
            
            y_true = y_true_all[i]
            y_pred = y_pred_all[i]
        
            ax = make_plot(y_true, y_pred, ax) 
        
        plt.tight_layout()     
        plt.savefig(f'{fig_save_folder}/{fig_name}.png')
        plt.show()   
        
        
    y_true_all = [[1, 2, 3], [1, 2, 3]]
    y_pred_all = [[1.1, 2, 3.1], [1, 1.9, 3]]
    
    fig_save_folder = './result'
    fig_name = 'test'
    plot_all(y_true_all, y_pred_all, fig_save_folder, fig_name, plot_size=(4,4), num_plots_x = 2)
    

    输出:

    【讨论】:

      猜你喜欢
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      相关资源
      最近更新 更多