gpt4 book ai didi

python - 在直方图中记录计数值,而不是轴本身

转载 作者:行者123 更新时间:2023-12-01 05:12:46 26 4
gpt4 key购买 nike

我制作这个例子是为了展示我正在尝试做的事情:

data = np.random.normal(loc=1000,scale=20,size=2000)
plt.hist(np.log10(data),log=True)
plt.xlabel('Log(data)')
plt.ylabel('Count')
plt.show()

执行此命令后,我得到了一个很好的直方图,其中 x 轴为 Log(data),y 轴为 Counts(y 轴为对数缩放)。出于某种原因,我希望在 y 尺度上记录(计数),我的意思不是记录轴,而是记录值本身。就像 Log(data) 与 Log(counts) 一样,轴本身不应该被记录。谢谢您的帮助。 In above fig, I don't want logged y-axis but the values themselves to be logged if that can be done

最佳答案

我不确定您是否可以使用 hist() 函数本身来执行此操作,但您可以使用条形图轻松地重新创建它。

import numpy as np
import matplotlib.pyplot as plt

data = np.random.normal(loc=1000,scale=20,size=2000)
n, bins, patches = plt.hist(np.log10(data),log=True)

# Extract the midpoints and widths of each bin.
bin_starts = bins[0:bins.size-1]
bin_widths = bins[1:bins.size] - bins[0:bins.size-1]

# Clear the histogram plot and replace it with a bar plot.
plt.clf()
plt.bar(bin_starts,np.log10(n),bin_widths)
plt.xlabel('Log(data)')
plt.ylabel('Log(Counts)')
plt.show()

Bar Chart

关于python - 在直方图中记录计数值,而不是轴本身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23838544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com