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

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

如何從 for 循環 matplotlib 中的子圖創建單個圖形

如何從 for 循環 matplotlib 中的子圖創建單個圖形

慕絲7291255 2023-08-22 18:00:07
我有 4 個 numpy 數組格式的圖像,其中每個圖像都是 4D(61、73、61、11),最后一個維度對應于圖像通道(在我的例子中為 11)。我使用 for 循環迭代通道,并在每次迭代時為每個圖像創建一個包含 4 個圖的子圖。在 jupyter 筆記本中,我可以看到所有子圖,但我想創建一個包含所有子圖的圖形,這樣我就可以創建一個 png 而不是 11。這是 matplotlib 中的代碼。import maplotlib.pyplot as pltcenter_slices = [s//2 for s in concat_img.shape[:1]] # take the middle sliceprint(np.squeeze(concat_img[center_slices[0], :, :, 5]).shape)for i in range(10):    f, axarr = plt.subplots(1, 4, figsize=(20,5),  sharex=True);    f.suptitle('Different intensity normalisation methods on brain fMRI image dual_regression + ALFF derivatives')    img = axarr[0].imshow(np.squeeze(concat_img[:, :, center_slices[0], i]), cmap='gray');    axarr[0].axis('off')    axarr[0].set_title('Original image')    f.colorbar(img, ax=axarr[0])    img = axarr[1].imshow(np.squeeze(concat_img_white[:, :, center_slices[0], i]), cmap='gray');    axarr[1].axis('off')    axarr[1].set_title('Zero mean/unit stdev')    f.colorbar(img, ax=axarr[1])    img = axarr[2].imshow(np.squeeze(concat_img_zero_one[:, :, center_slices[0], i]), cmap='gray');    axarr[2].axis('off')    axarr[2].set_title('[0,1] rescaling')    f.colorbar(img, ax=axarr[2])    img = axarr[3].imshow(np.squeeze(concat_img_one_one[:, :, center_slices[0], i]), cmap='gray');    axarr[3].axis('off')    axarr[3].set_title('[-1,1] rescaling')    f.colorbar(img, ax=axarr[3])        f.subplots_adjust(wspace=0.05, hspace=0, top=0.8)#     plt.savefig('./TTT.{0:07d}.png'.format(i)) # save each subplot in png plt.show();   
查看完整描述

1 回答

?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

這可以通過使用 創建一個坐標區實例來完成nrows != 1。我在下面附上了一個例子。


import matplotlib.pyplot as plt

import numpy as np


nrows = 5

ncols = 4


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

ydata = 1 * xdata

X, Y = np.meshgrid(xdata, ydata)

zdata = np.sin(X + Y)


fig, ax = plt.subplots(nrows=nrows, ncols=ncols, sharex=True,

                       figsize=(nrows * 2.2, 2 * ncols))


for j in range(nrows):

    for i in range(ncols):

        cbar = ax[j, i].contourf(zdata)

        fig.colorbar(cbar, ax=ax[j, i])

        

fig.tight_layout()


查看完整回答
反對 回復 2023-08-22
  • 1 回答
  • 0 關注
  • 207 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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