轉(zhuǎn)載:用matplotlib實(shí)現(xiàn)畫中畫

當(dāng)我們想要在一個(gè)坐標(biāo)系中包含另外一個(gè)完整的圖像時(shí),就需要用到子圖相關(guān)的技術(shù),在matplotlib中,提供了以下兩種實(shí)現(xiàn)方式

1. 畫中畫

通過(guò)在原本axes中插入一個(gè)新的axes, 來(lái)實(shí)現(xiàn)畫中畫的目的,代碼如下

fig,ax = plt.subplots()
ax.scatter(x = np.random.randn(10) ,  y = np.random.randn(10) , s = 40 * np.arange(10)  ,c=np.random.randn(10))
ax1 = ax.inset_axes([0.6, 0.5, 0.3, 0.45])
ax1.scatter(x = np.random.randn(10) , y = np.random.randn(10) , s = 40 * np.arange(10) , c=np.random.randn(10))
plt.show()

輸出結(jié)果如下

新的axes

這種方式本質(zhì)上兩個(gè)axes是相互獨(dú)立的,所以可以實(shí)現(xiàn)任意兩個(gè)圖像的疊加,非常的靈活。

2. 縮放圖

顧名思義,對(duì)圖中的局部區(qū)域進(jìn)行縮放,屬于畫中畫的一種特殊情況,在matplotlib中,針對(duì)這一場(chǎng)景,專門提供了indicate_inset_zoom,來(lái)實(shí)現(xiàn)縮放圖,用法如下

seed = 123456
np.random.seed(seed)
ax.scatter(x = np.random.randn(10) , y = np.random.randn(10) , s = 40 * np.arange(10) , c = np.random.randn(10))
ax1 = ax.inset_axes([0.6, 0.5, 0.32, 0.45])
np.random.seed(seed)
ax1.scatter(x = np.random.randn(10) , y=np.random.randn(10) , s = 40 * np.arange(10) , c = np.random.randn(10))
ax1.set_xlim(-1.5, -0.8)
ax1.set_ylim(-0.8, -0.3)
ax.indicate_inset_zoom(ax1)
plt.show()

輸出結(jié)果如下

局部縮放

在縮放圖中,會(huì)將原本的區(qū)域和縮放之后的區(qū)域用線條連接起來(lái),這是縮放圖獨(dú)有的特征。

·end·

轉(zhuǎn)載自:https://cloud.tencent.com/developer/article/1692283

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容