【问题标题】:How to insert a figure (3x3 subplot) into a subplot in matplotlib python如何将图形(3x3 子图)插入 matplotlib python 中的子图
【发布时间】:2019-10-04 05:20:45
【问题描述】:

我想在子图中插入一个 3x3 图形。 Herehere 显示了类似的问题,但解决方案似乎不起作用(我认为)。

谁能提供一些代码(尽可能简单)来生成这个:

如果有人可以提供帮助,我会很高兴,在此先感谢。任何答案或评论将不胜感激。

【问题讨论】:

    标签: python matplotlib insert figure subplot


    【解决方案1】:

    我使用this 答案创建了一个变通解决方案。我添加的部分写在注释# 的行下方。我承认这并不普遍和完美,但在我看来仍然足以完成工作。

    import matplotlib.pyplot as plt
    import numpy as np
    
    def add_subplot_axes(ax, rect): # This is the function in the linked answer
        fig = plt.gcf()
        box = ax.get_position()
        width = box.width
        height = box.height
        inax_position  = ax.transAxes.transform(rect[0:2])
        transFigure = fig.transFigure.inverted()
        infig_position = transFigure.transform(inax_position)    
        x = infig_position[0]
        y = infig_position[1]
        width *= rect[2]
        height *= rect[3]  
        subax = fig.add_axes([x,y,width,height])
        x_labelsize = subax.get_xticklabels()[0].get_size()
        y_labelsize = subax.get_yticklabels()[0].get_size()
        x_labelsize *= rect[2]**0.5
        y_labelsize *= rect[3]**0.5
        subax.xaxis.set_tick_params(labelsize=x_labelsize)
        subax.yaxis.set_tick_params(labelsize=y_labelsize)
        return subax
    
    # Modified part below
    
    fig = plt.figure(figsize=(8,6))
    ax = fig.add_subplot(111)
    x_start, y_start = 0.4, 0.4
    for i in range(3):
        for j in range(3):
            rect = [x_start+0.2*i, y_start+0.2*j, 0.15, 0.15]
            ax_ = add_subplot_axes(ax,rect)
            ax_.tick_params(labelsize=6)
    plt.show()
    

    【讨论】:

    • 我很惊讶,这是一种“日常”任务。必须有一种更简单的方法来做到这一点,对吧?
    • @Alfredo:在我看来这并不常见。通常的做法是在有多种方法的图形中插入一个插图。但我自己第一次看到这样的子图网格一个情节。可能有一些内置功能用于绘图内的此类网格,我不知道
    猜你喜欢
    • 1970-01-01
    • 2014-08-29
    • 2017-06-20
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 2020-05-05
    相关资源
    最近更新 更多