Ada*_*ser 22 python matplotlib normalize
我正在使用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)
Run Code Online (Sandbox Code Playgroud)
我尝试过使用cb.set_array,但这似乎没有做任何事情,而cb.set_clim,但是它完全重新调整了颜色.
Ste*_*joa 40
就在这里!使用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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
44127 次 |
| 最近记录: |