python中的对数y轴箱子

时间:2022-02-15 14:58:21

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轴)绘制它,我不确定为什么下面的代码不起作用:

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中添加log =true而不是pl .yscale(“log”)。hist line和我也尝试了ax.set_yscale('log'),但似乎什么都不管用。我要么得到一个空的图,要么y轴确实是对数的(如上面所示的代码),但是没有数据绘制(没有箱子)。

2 个解决方案

#1


61  

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和默认的是面具的点(日志(0)- >定义)在做对数转换(讨论改变这一状况,但我不记得了),所以当你试图画出矩形条情节,底部边缘是蒙面- >没有矩形。

#2


3  

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返回[1-10]中的bin,以对数间隔-在我的例子中,xx是一个npvector >,所以下面的代码可以实现这个目的

logbins=np.max(xx)*(np.logspace(0, 1, num=1000) - 1)/9
hh,ee=np.histogram(xx, density=True, bins=logbins)

#1


61  

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和默认的是面具的点(日志(0)- >定义)在做对数转换(讨论改变这一状况,但我不记得了),所以当你试图画出矩形条情节,底部边缘是蒙面- >没有矩形。

#2


3  

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返回[1-10]中的bin,以对数间隔-在我的例子中,xx是一个npvector >,所以下面的代码可以实现这个目的

logbins=np.max(xx)*(np.logspace(0, 1, num=1000) - 1)/9
hh,ee=np.histogram(xx, density=True, bins=logbins)