亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在matplotlib的子圖中嵌入小圖

在matplotlib的子圖中嵌入小圖

DIEA 2019-10-28 14:30:59
如果要在較大的圖中插入一個較小的圖,則可以使用Axes,例如此處。問題是我不知道如何在子圖中執行相同的操作。我有幾個子圖,我想在每個子圖中繪制一個小圖。示例代碼將如下所示:import numpy as npimport matplotlib.pyplot as pltfig = plt.figure()for i in range(4):    ax = fig.add_subplot(2,2,i)    ax.plot(np.arange(11),np.arange(11),'b')    #b = ax.axes([0.7,0.7,0.2,0.2])     #it gives an error, AxesSubplot is not callable    #b = plt.axes([0.7,0.7,0.2,0.2])    #plt.plot(np.arange(3),np.arange(3)+11,'g')    #it plots the small plot in the selected position of the whole figure, not inside the subplot有任何想法嗎?提前致謝!
查看完整描述

3 回答

?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

我寫了一個非常類似于plt.axes的函數。您可以使用它來繪制子子圖。有一個例子...


import matplotlib.pyplot as plt

import numpy as np


def add_subplot_axes(ax,rect,axisbg='w'):

    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]  # <= Typo was here

    subax = fig.add_axes([x,y,width,height],axisbg=axisbg)

    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


def example1():

    fig = plt.figure(figsize=(10,10))

    ax = fig.add_subplot(111)

    rect = [0.2,0.2,0.7,0.7]

    ax1 = add_subplot_axes(ax,rect)

    ax2 = add_subplot_axes(ax1,rect)

    ax3 = add_subplot_axes(ax2,rect)

    plt.show()


def example2():

    fig = plt.figure(figsize=(10,10))

    axes = []

    subpos = [0.2,0.6,0.3,0.3]

    x = np.linspace(-np.pi,np.pi)

    for i in range(4):

        axes.append(fig.add_subplot(2,2,i))

    for axis in axes:

        axis.set_xlim(-np.pi,np.pi)

        axis.set_ylim(-1,3)

        axis.plot(x,np.sin(x))

        subax1 = add_subplot_axes(axis,subpos)

        subax2 = add_subplot_axes(subax1,subpos)

        subax1.plot(x,np.sin(x))

        subax2.plot(x,np.sin(x))

if __name__ == '__main__':

    example2()

    plt.show()



查看完整回答
反對 回復 2019-10-28
?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

您現在可以使用matplotlibs inset_axes方法執行此操作(請參閱docs):


from mpl_toolkits.axes_grid.inset_locator import inset_axes

inset_axes = inset_axes(parent_axes,

                    width="30%", # width = 30% of parent_bbox

                    height=1., # height : 1 inch

                    loc=3)

更新:正如Kuti所指出的,對于matplotlib 2.1版或更高版本,您應該將import語句更改為:


from mpl_toolkits.axes_grid1.inset_locator import inset_axes

現在,還有一個完整的示例顯示了所有可用的選項。


查看完整回答
反對 回復 2019-10-28
  • 3 回答
  • 0 關注
  • 1945 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號