如何使用代表原始数据的颜色条绘制对数归一化 imshow 图

发布于 2024-08-26 23:15:33 字数 817 浏览 9 评论 0原文

我正在使用 matplotlib 绘制对数归一化图像,但我希望原始原始图像数据在颜色条中表示,而不是在 [0-1] 区间中表示。我感觉有一种更 matplotlib'y 的方法可以通过使用某种标准化对象而不事先转换数据来做到这一点......在任何情况下,原始图像中都可能存在负值。

import matplotlib.pyplot as plt
import numpy as np

def log_transform(im):
    '''returns log(image) scaled to the interval [0,1]'''
    try:
        (min, max) = (im[im > 0].min(), im.max())
        if (max > min) and (max > 0):
            return (np.log(im.clip(min, max)) - np.log(min)) / (np.log(max) - np.log(min))
    except:
        pass
    return im

a = np.ones((100,100))
for i in range(100): a[i] = i
f = plt.figure()
ax = f.add_subplot(111)
res = ax.imshow(log_transform(a))
# the colorbar drawn shows [0-1], but I want to see [0-99]
cb = f.colorbar(res)

我尝试过使用 cb.set_array,但似乎没有做任何事情,而 cb.set_clim,但完全重新调整了颜色。

I'm using matplotlib to plot log-normalized images but I would like the original raw image data to be represented in the colorbar rather than the [0-1] interval. I get the feeling there's a more matplotlib'y way of doing this by using some sort of normalization object and not transforming the data beforehand... in any case, there could be negative values in the raw image.

import matplotlib.pyplot as plt
import numpy as np

def log_transform(im):
    '''returns log(image) scaled to the interval [0,1]'''
    try:
        (min, max) = (im[im > 0].min(), im.max())
        if (max > min) and (max > 0):
            return (np.log(im.clip(min, max)) - np.log(min)) / (np.log(max) - np.log(min))
    except:
        pass
    return im

a = np.ones((100,100))
for i in range(100): a[i] = i
f = plt.figure()
ax = f.add_subplot(111)
res = ax.imshow(log_transform(a))
# the colorbar drawn shows [0-1], but I want to see [0-99]
cb = f.colorbar(res)

I've tried using cb.set_array, but that didn't appear to do anything, and cb.set_clim, but that rescales the colors completely.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左耳近心 2024-09-02 23:15:33

是的,有!使用LogNorm。以下是我编写的一个实用程序的代码摘录,该实用程序用于在对数刻度上显示混淆矩阵。

from pylab import figure, cm
from matplotlib.colors import LogNorm

# C = some matrix
f = figure(figsize=(6.2, 5.6))
ax = f.add_axes([0.17, 0.02, 0.72, 0.79])
axcolor = f.add_axes([0.90, 0.02, 0.03, 0.79])

im = ax.matshow(C, cmap=cm.gray_r, norm=LogNorm(vmin=0.01, vmax=1))

t = [0.01, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0]
f.colorbar(im, cax=axcolor, ticks=t, format="$%.2f$")

f.show()

Yes, there is! Use LogNorm. Here is a code excerpt from a utility that I wrote to display confusion matrices on a log scale.

from pylab import figure, cm
from matplotlib.colors import LogNorm

# C = some matrix
f = figure(figsize=(6.2, 5.6))
ax = f.add_axes([0.17, 0.02, 0.72, 0.79])
axcolor = f.add_axes([0.90, 0.02, 0.03, 0.79])

im = ax.matshow(C, cmap=cm.gray_r, norm=LogNorm(vmin=0.01, vmax=1))

t = [0.01, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0]
f.colorbar(im, cax=axcolor, ticks=t, format="$%.2f
quot;)

f.show()
酒废 2024-09-02 23:15:33

如果您只想对图像进行对数归一化(以增强细节),而不是对数据进行对数归一化(以保留物理值),那么您必须对颜色图本身应用转换。您可以使用说明书中给出的函数 cmap_map() 来做到这一点:
https://scipy-cookbook.readthedocs.io/items/Matplotlib_ColormapTransformations.html

If you just want the image to be log-normalized (to enhance details), but not the data (to preserve physical values), then you have to apply the transformation on the colormap itself. You can do that with the function cmap_map() given in the cookbook:
https://scipy-cookbook.readthedocs.io/items/Matplotlib_ColormapTransformations.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文