対数正規化された画像をプロットするために matplotlib を使用していますが、元の生の画像データを [0-1] 間隔ではなくカラーバーで表現したいと考えています。ある種の正規化オブジェクトを使用し、事前にデータを変換しないことで、これを行うよりmatplotlibの方法があるように感じます...いずれにせよ、生の画像に負の値が含まれる可能性があります。
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 は色を完全に再スケーリングします。