“log=True”在matplotlib中实际做了什么?

2024-09-30 08:17:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我的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)

为什么n2与{}不同?我以为log = True按对数比例缩放。。但它没有。那它在做什么呢?bins2bins1 = np.log(bins)也会发生同样的情况。在

编辑 这个

^{pr2}$

给了我

a

然后通过做

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

我得到

b


Tags: importlogtruematplotlibasnppltsigma
1条回答
网友
1楼 · 发布于 2024-09-30 08:17:12

当您设置log=True时,直方图轴(不是返回参数)以log-scale为单位。对于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()

会回来的

^{pr2}$

相关问题 更多 >

    热门问题