什么`log = True`实际上在matplotlib中做了什么?

时间:2017-03-29 17:20:59

标签: python matplotlib histogram logarithm

我的MCVE:

    import numpy as np
    import matplotlib.mlab as mlab
    import matplotlib.pyplot as plt

    #normal sample
    mu, sigma = 100, 15
    x = mu + sigma*np.random.randn(10000)

    #histogram
    n, bins, patches = plt.hist(x, 50)
    plt.axis([40, 160, 0, 800])
    n2, bins2, patches2 = plt.hist(x,50,log=True)
    n1 = np.log(n)

现在为什么n2n1不同?我认为log = True以对数方式缩放所有内容......但事实并非如此。那它在做什么?同样的事情发生在bins2bins1 = np.log(bins)

修改 这个

    mu, sigma = 100, 15
    x = mu + sigma*np.random.randn(10000)
    hist, bin_edges = np.histogram(x,50)
    nwidth = bin_edges[7]-bin_edges[6]   #just difference between two random bins
    plt.bar(np.delete(bin_edges,len(bin_edges)-1),hist,nwidth)  #so I have the right number of bins
    plt.show()

给了我

a

然后做

histsq = np.sqrt(np.log(hist))
plt.bar(np.delete(bin_edges,len(bin_edges)-1),histsq,nwidth)
plt.show()

我获得了

b

1 个答案:

答案 0 :(得分:1)

设置log = True时,直方图轴(不是返回参数)是对数刻度。对于log = True和log = False,返回参数(n,bins),即箱的值和箱的边缘是相同的。这意味着n == n2和bins == bins2都是真的。

请参阅下面的代码,看看是否属实

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

#normal sample
mu, sigma = 100, 15
x = mu + sigma*np.random.randn(10000)

#histogram
plt.subplot(3, 1, 1)
n, bins, patches = plt.hist(x, 50)
# plt.axis([40, 160, 0, 800])


plt.subplot(3, 1, 2)
n2, bins2, patches2 = plt.hist(x,50,log=True)

if (all(n==n2) and all(bins==bins2)):
    print 'Return parameters of hist with log option or without log option are always the same'

#if you want to apply two transformation on the return data and visualize it.
n1 = np.log(n)
plt.subplot(3, 1, 3)
xx = bins[1:]
yy = np.sqrt(n1)
plt.bar(xx,yy, width = xx[1]-xx[0])

# square root of inverted parabola is not a linear line, but has some curvature

x = np.arange(-1,1.1,.1)
y = 10-(x)**2 # inverted parabola
y1 = np.sqrt(y) # square of inverted parabola

fig, ax1 = plt.subplots()
ax1.plot(x, y, 'b-')
# Make the y-axis label, ticks and tick labels match the line color.
ax1.set_ylabel('inverted parabola', color='b')
ax1.tick_params('y', colors='b')

ax2 = ax1.twinx()
ax2.plot(x, y1, 'r.')
ax2.set_ylabel('sqrt of inverted parabola', color='r')
ax2.tick_params('y', colors='r')
fig.tight_layout()
plt.show()

将返回

Return parameters of hist method with log option or without log option are always the same