如何通过自定义刻度和对数变换查看所有颜色条标签?

时间:2014-07-28 16:01:44

标签: matplotlib logarithm colorbar

使用自定义刻度和LogNorm()时,仅显示10个标签的功率,而不是所有标签。

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from numpy.random import random

# Make plot with horizontal colorbar
fig, ax = plt.subplots()

data = random((250,250)) + 3.5

norm = matplotlib.colors.LogNorm(vmin=0.1,vmax=10)

cax = ax.imshow(data, interpolation='nearest', cmap=cm.afmhot, norm=norm)
ax.set_title('Gaussian noise with horizontal colorbar')

cbar = fig.colorbar(cax, ticks=[0.1,0.2,0.5,1,2,5,10], orientation='horizontal')

plt.savefig("example.png")

enter image description here

1 个答案:

答案 0 :(得分:2)

您必须使用format选项:

cbar = fig.colorbar(cax, ticks=[0.1,0.2,0.5,1,2,5,10], 
                    orientation='horizontal', format='%.1f')

这远离表示为10^n(例如与0.5斗争)到浮点数:

all ticks labeled