matplotlib:在hist2d中对计数进行对数变换
有没有简单的方法可以在使用matplotlib绘制二维直方图时得到对数变换的计数?和pyplot.hist
方法不同,pyplot.hist2d
方法似乎没有对数参数。
目前我正在这样做:
import numpy as np
import matplotlib as mpl
import matplotlib.pylab as plt
matrix, *opt = np.histogram2d(x, y)
img = plt.imshow(matrix, norm = mpl.colors.LogNorm(), cmap = mpl.cm.gray,
interpolation="None")
这样绘制出的直方图是预期的,但坐标轴的标签显示的是箱子的索引,而不是我们想要的值。
1 个回答
44
这有点尴尬,但我问题的答案其实在对应代码的文档注释
里:
Notes
-----
Rendering the histogram with a logarithmic color scale is
accomplished by passing a :class:`colors.LogNorm` instance to
the *norm* keyword argument. Likewise, power-law normalization
(similar in effect to gamma correction) can be accomplished with
:class:`colors.PowerNorm`.
所以这样做是有效的:
import matplotlib as mpl
import matplotlib.pylab as plt
par = plt.hist2d(x, y, norm=mpl.colors.LogNorm(), cmap=mpl.cm.gray)