Reputation: 31349
Is there a simple way to get log transformed counts when plotting a two dimensional histogram in matplotlib? Unlike the pyplot.hist
method, the pyplot.hist2d
method does not seem to have a log parameter.
Currently I'm doing the following:
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")
Which plots the expected histogram, but the axis labels show the indices of the bins and thus not the expected value.
Upvotes: 25
Views: 30696
Reputation: 31349
It's kind of embarrassing, but the answer to my question is actually in the docstring
of the corresponding code:
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`.
So this works:
import matplotlib as mpl
import matplotlib.pylab as plt
par = plt.hist2d(x, y, norm=mpl.colors.LogNorm(), cmap=mpl.cm.gray)
Upvotes: 46