matplotlib:在 hist2d 中记录转换计数 [英] matplotlib: log transform counts in hist2d

查看:67
本文介绍了matplotlib:在 hist2d 中记录转换计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在matplotlib中绘制二维直方图时,是否有一种简单的方法来获取对数转换后的计数?与 pyplot.hist 方法不同,pyplot.hist2d 方法似乎没有有一个log参数.

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")

其中绘制了预期的直方图,但轴标签显示了bin的索引,因此没有预期的值.

Which plots the expected histogram, but the axis labels show the indices of the bins and thus not the expected value.

推荐答案

有点尴尬,不过我问题的答案其实在

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`.

所以这是有效的:

import matplotlib as mpl
import matplotlib.pylab as plt
par = plt.hist2d(x, y, norm=mpl.colors.LogNorm(), cmap=mpl.cm.gray)

这篇关于matplotlib:在 hist2d 中记录转换计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆