[英]How to extract data from plt.imshow() or plt.matshow()?
要獲取您在 matplotlib 軸上繪制的圖像的 RGBA 數組,首先,您要獲取圖像對象(此處為im3
)。 其次,獲取它的顏色圖(這里是ccmap
)。 最后一步,將數據數組im3._A
傳遞給ccmap
。
import matplotlib.cm as cm
import numpy as np
import matplotlib.pyplot as plt
data = np.random.random((10,10))
# imshow or matshow is OK
#im3 = plt.imshow(data, cmap="viridis_r") #any colormap will do
im3 = plt.matshow(data, cmap="viridis_r")
plt.axis('off')
#plt.savefig("question2.png",bbox_inches='tight',pad_inches=0)
plt.show()
# get the colormap used by the previous imshow()
ccmap = im3.get_cmap()
print(ccmap.name) # 'viridis_r'
# get the image data ***YOU ASK FOR THIS***
img_rgba_array = ccmap(im3._A)
# plot the image data
ax = plt.subplot(111)
ax.imshow(img_rgba_array); #dont need any cmap to plot
示例輸出圖:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.