Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python柱状图中设置对数容器_Python_Numpy_Matplotlib_Histogram - Fatal编程技术网

如何在Python柱状图中设置对数容器

如何在Python柱状图中设置对数容器,python,numpy,matplotlib,histogram,Python,Numpy,Matplotlib,Histogram,据我所知,直方图函数中的Log=True选项仅指y轴 P.hist(d,bins=50,log=True,alpha=0.5,color='b',histtype='step') 我需要垃圾箱在log10中均匀分布。有什么方法可以做到这一点吗?使用logspace()创建几何序列,并将其传递给bins参数。并将xaxis的比例设置为对数比例 import pylab as pl import numpy as np data = np.random.normal(size=10000) pl

据我所知,直方图函数中的Log=True选项仅指y轴

P.hist(d,bins=50,log=True,alpha=0.5,color='b',histtype='step')
我需要垃圾箱在log10中均匀分布。有什么方法可以做到这一点吗?

使用logspace()创建几何序列,并将其传递给bins参数。并将xaxis的比例设置为对数比例

import pylab as pl
import numpy as np

data = np.random.normal(size=10000)
pl.hist(data, bins=np.logspace(np.log10(0.1),np.log10(1.0), 50))
pl.gca().set_xscale("log")
pl.show()
import numpy as np
import matplotlib.pyplot as plt

data = 10**np.random.normal(size=500)

_, bins = np.histogram(np.log10(data + 1), bins='auto')
plt.hist(data, bins=10**bins);
plt.gca().set_xscale("log")

最直接的方法是只计算限制的log10,计算线性间隔的箱子,然后通过提高到10的幂进行转换,如下所示:

import pylab as pl
import numpy as np

data = np.random.normal(size=10000)

MIN, MAX = .01, 10.0

pl.figure()
pl.hist(data, bins = 10 ** np.linspace(np.log10(MIN), np.log10(MAX), 50))
pl.gca().set_xscale("log")
pl.show()

除了上述内容,在熊猫数据帧上执行此操作也同样有效:

some_column_hist = dataframe['some_column'].plot(bins=np.logspace(-2, np.log10(max_value), 100), kind='hist', loglog=True, xlim=(0,max_value))
我要提醒大家,标准化垃圾箱可能会有问题。每个箱子都比前一个箱子大,因此必须除以它的大小,以便在绘图之前规范化频率,而我的解决方案和HYRY的解决方案似乎都不能解释这一点


来源:

以下代码说明了如何将
bins='auto'
与对数刻度一起使用

import pylab as pl
import numpy as np

data = np.random.normal(size=10000)
pl.hist(data, bins=np.logspace(np.log10(0.1),np.log10(1.0), 50))
pl.gca().set_xscale("log")
pl.show()
import numpy as np
import matplotlib.pyplot as plt

data = 10**np.random.normal(size=500)

_, bins = np.histogram(np.log10(data + 1), bins='auto')
plt.hist(data, bins=10**bins);
plt.gca().set_xscale("log")

注意
np.logspace(0.1,1.0,…)
将创建一个从
10**0.1
10**1.0
的范围,而不是从
0.1
1.0
的范围。logspace(np.log10(0.1),np.log10(1.0),50)有关如何使用bin='auto'@AndreHolzner@orangeherbet直接指定端点的方法,请参见我的答案。如果这样做,您必须将每个bin中的计数除以bin宽度!