这是一个关于matplotlib图像中颜色信息的问题
我用以下代码绘制了两个数组:
import numpy as np
import matplotlib.pyplot as plt
M1 = ([1, 2, 3, np.nan],
[4, 5, np.nan, np.nan],
[6, 7, 8, 9])
M2 = ([np.nan, np.nan, np.nan, np.nan],
[np.nan, 1, 2, 3],
[np.nan, 4, 5, 6])
M1arr = ~np.isnan(M1)
M2arr = ~np.isnan(M2)
fig, ax = plt.subplots()
im1 = ax.imshow(M1arr, cmap="Reds", alpha=0.5)
im2 = ax.imshow(M2arr, cmap="Blues", alpha=0.5)
#color_array = mystery_function(im1, im2, ax, fig)
plt.show()
有没有一种方法可以从我们最终看到的打印复合图像中提取颜色(例如,创建一个色条)?我已经看到了如何从{im2
并不是我们最终将看到的复合叠加图像,im1
和im2
似乎只是由plt.show()
组合而成。我还试图强制matplotlib使用plt.draw()
预先生成最终图像,并使用ax.get_images()
提取图像,可惜这两个图像仍然是分开的
我对如何以不同的方式解决这个问题不感兴趣——在无用的尝试之后,我改变了策略并plotted the combined matrix instead。我的问题是,如果我们能在AxesImage
显示前不久从中提取出四种颜色。
关于matplotlib如何组合覆盖中的颜色的信息也同样有用。我尝试了im1
和im2
中的颜色总和(显然是错误的,因为它可以超过1)和每个颜色通道的平均值(也错误)
经过一些挖掘,我找到了如何获得渲染数组的答案。正如我之前尝试的那样,它基于绘制图形,并使用^{} 检索渲染器缓冲区的memoryview:
输出:
为了在颜色栏中使用识别出的唯一颜色,我绘制了一个临时图像,稍后将其删除。人们也可以从头开始画一个颜色条,但手工控制所有参数却很烦人
更新
我只是注意到颜色条不是必需的,它只是在previous question中很方便。因此,我们也可以创建一个带有补丁的人物图例:
输出:
带回家的消息:从后端内存中回溯提取信息要比从后端内存中提取信息容易得多
相关问题 更多 >
编程相关推荐