使用LogNorm()时,Colorbar不显示值

时间:2011-12-08 21:34:32

标签: python numpy matplotlib contour colorbar

我正在尝试制作轮廓图,其轮廓水平由值的对数缩放。但是,颜色条在颜色旁边没有显示足够的值。这是一个简单的例子。

import numpy as N
import matplotlib as M
import matplotlib.pyplot as PLT

# Set up a simple function to plot 
values = N.empty((10,10))
for xi in range(10):
    for yi in range(10):
        values[xi,yi] = N.exp(xi*yi/10. - 1)

levels = N.logspace(-1, 4, 10)
log_norm = M.colors.LogNorm() 
# Currently not used - linear scaling
linear_norm = M.colors.Normalize()

# Plot the function using the indices as the x and y axes
PLT.contourf(values, norm=log_norm, levels=levels)
PLT.colorbar()

如果在contourf调用中切换log_norm for linear_norm,您将看到颜色条确实有值。当然,使用linear_norm意味着颜色是线性缩放的,并且轮廓没有很好地分布用于此功能。

我在Mac OS 10.7上使用python 2.7.2,mathlotlib附带的enthought版本。

1 个答案:

答案 0 :(得分:4)

PLT.colorbar的调用添加格式:

import numpy as N
import matplotlib as M
import matplotlib.pyplot as PLT

# Set up a simple function to plot 
x,y = N.meshgrid(range(10),range(10))
values = N.exp(x*y/10. - 1)

levels = N.logspace(-1, 4, 10)
log_norm = M.colors.LogNorm() 
# Currently not used - linear scaling
linear_norm = M.colors.Normalize()
# Plot the function using the indices as the x and y axes
PLT.contourf(values, norm=log_norm, levels=levels)
PLT.colorbar(format='%.2f')
PLT.show()

enter image description here