使用LogNorm()时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换成linear_norm,你会发现颜色条旁边有数值。当然,使用linear_norm意味着颜色是线性缩放的,这样等高线在这个函数上分布就不太好了。
我使用的是python 2.7.2,enthought版本,里面自带了matplotlib,运行在Mac OS 10.7上。
1 个回答
5
在调用 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()