Python 使用 Matplotlib 在对数刻度上绘制直方图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47850202/
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
plotting a histogram on a Log scale with Matplotlib
提问by Tommy
I have a pandas DataFrame that has the following values in a Series
我有一个 Pandas DataFrame,它在一个系列中具有以下值
x = [2, 1, 76, 140, 286, 267, 60, 271, 5, 13, 9, 76, 77, 6, 2, 27, 22, 1, 12, 7, 19, 81, 11, 173, 13, 7, 16, 19, 23, 197, 167, 1]
I was instructed to plot two histograms in a Jupyter notebook with Python 3.6. No sweat right?
我被指示使用 Python 3.6 在 Jupyter notebook 中绘制两个直方图。不会出汗吧?
x.plot.hist(bins=8)
plt.show()
I chose 8 bins because that looked best to me. I have also been instructed to plot another histogram with the log of x.
我选择了 8 个垃圾箱,因为这对我来说看起来最好。我还被指示用 x 的对数绘制另一个直方图。
x.plot.hist(bins=8)
plt.xscale('log')
plt.show()
This histogram looks TERRIBLE. Am I not doing something right? I've tried fiddling around with the plot, but everything I've tried just seems to make the histogram look even worse. Example:
这个直方图看起来很糟糕。我做的不对吗?我试过摆弄情节,但我试过的一切似乎都让直方图看起来更糟。例子:
x.plot(kind='hist', logx=True)
I was not given any instructions other than plot the log of X as a histogram.
除了将 X 的对数绘制为直方图之外,我没有得到任何指示。
I really appreciate any help!!!
我真的很感激任何帮助!!!
For the record, I have imported pandas, numpy, and matplotlib and specified that the plot should be inline.
作为记录,我导入了 pandas、numpy 和 matplotlib,并指定该图应该是内联的。
回答by ImportanceOfBeingErnest
Specifying bins=8
in the hist
call means that the range between the minimum and maximum value is divided equally into 8 bins. What is equal on a linear scale is distorted on a log scale.
bins=8
在hist
调用中指定意味着最小值和最大值之间的范围被平均分为 8 个 bin。在线性尺度上相等的东西在对数尺度上是扭曲的。
What you could do is specify the bins of the histogram such that they are unequal in width in a way that would make them look equal on a logarithmic scale.
您可以做的是指定直方图的 bin,使它们的宽度不相等,使它们在对数刻度上看起来相等。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
x = [2, 1, 76, 140, 286, 267, 60, 271, 5, 13, 9, 76, 77, 6, 2, 27, 22, 1, 12, 7,
19, 81, 11, 173, 13, 7, 16, 19, 23, 197, 167, 1]
x = pd.Series(x)
# histogram on linear scale
plt.subplot(211)
hist, bins, _ = plt.hist(x, bins=8)
# histogram on log scale.
# Use non-equal bin sizes, such that they look equal on log scale.
logbins = np.logspace(np.log10(bins[0]),np.log10(bins[-1]),len(bins))
plt.subplot(212)
plt.hist(x, bins=logbins)
plt.xscale('log')
plt.show()
回答by Tommy
plot another histogram with the log of x.
用 x 的对数绘制另一个直方图。
is not the same as plotting x on the logarithmic scale. Plotting the logarithm of x would be
与在对数刻度上绘制 x 不同。绘制 x 的对数将是
np.log(x).plot.hist(bins=8)
plt.show()
The difference is that the values of x themselves were transformed: we are looking at their logarithm.
不同之处在于 x 本身的值被转换了:我们正在查看它们的对数。
This is different from plotting on the logarithmic scale, where we keep x the same but change the way the horizontal axis is marked up (which squeezes the bars to the right, and stretches those to the left).
这与在对数刻度上绘图不同,在对数刻度上我们保持 x 不变,但改变了水平轴的标记方式(将条形向右挤压,向左拉伸)。
回答by Rahul Shaw
Here is one more solution without using a subplot or plotting two things in the same image.
这是另一种解决方案,不使用子图或在同一图像中绘制两件事。
import numpy as np
import matplotlib.pyplot as plt
def plot_loghist(x, bins):
hist, bins = np.histogram(x, bins=bins)
logbins = np.logspace(np.log10(bins[0]),np.log10(bins[-1]),len(bins))
plt.hist(x, bins=logbins)
plt.xscale('log')
plot_loghist(np.random.rand(200), 10)