python中的对数y轴箱
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17952279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Logarithmic y-axis bins in python
提问by mannaroth
I'm trying to create a histogram of a data column and plot it logarithmically (y-axis
) and I'm not sure why the following code does not work:
我正在尝试创建数据列的直方图并按对数 ( y-axis
)绘制它,但我不确定为什么以下代码不起作用:
import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt('foo.bar')
fig = plt.figure()
ax = fig.add_subplot(111)
plt.hist(data, bins=(23.0, 23.5,24.0,24.5,25.0,25.5,26.0,26.5,27.0,27.5,28.0))
ax.set_xlim(23.5, 28)
ax.set_ylim(0, 30)
ax.grid(True)
plt.yscale('log')
plt.show()
I've also tried instead of plt.yscale('log')
adding Log=true
in the plt.hist
line and also I tried ax.set_yscale('log')
, but nothing seems to work. I either get an empty plot, either the y-axis
is indeed logarithmic (with the code as shown above), but there is no data plotted (no bins).
我也试过而不是在行中plt.yscale('log')
添加Log=true
,plt.hist
我也试过ax.set_yscale('log')
,但似乎没有任何效果。我要么得到一个空图,要么y-axis
确实是对数(使用如上所示的代码),但没有绘制数据(没有箱)。
采纳答案by tacaswell
try
尝试
plt.yscale('log', nonposy='clip')
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.yscale
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.yscale
The issue is with the bottom of bars being at y=0 and the default is to mask out in-valid points (log(0)
-> undefined) when doing the log transformation (there was discussion of changing this, but I don't remember which way it went) so when it tries to draw the rectangles for you bar plot, the bottom edge is masked out -> no rectangles.
问题是条形底部在 y=0 处,默认值是log(0)
在进行日志转换时屏蔽无效点(-> 未定义)(讨论过更改此设置,但我不记得是哪种方式)它去了)所以当它试图为你的条形图绘制矩形时,底部边缘被屏蔽 - >没有矩形。
回答by Luca Rigazio
np.logspace returns bins in [1-10]
, logarithmically spaced - in my case xx is a npvector >0 so the following code does the trick
np.logspace 返回 bins in [1-10]
,对数间隔 - 在我的例子中 xx 是一个 npvector >0 所以下面的代码可以解决问题
logbins=np.max(xx)*(np.logspace(0, 1, num=1000) - 1)/9
hh,ee=np.histogram(xx, density=True, bins=logbins)