【发布时间】:2023-03-05 09:42:01
【问题描述】:
我想创建一个样本的频率图,其值介于 -1 和 1 之间。
使用 numpy 创建直方图效果很好:
freq, bins = np.histogram(sample, bins=np.arange(-1,1,0.05) )
但使用相同的箱创建绘图会给我一个错误(见标题):
plt.hist(freq, range=bins)
除此之外,如何调整 x 标签以显示正确的 bin 值?
最小的工作示例:
import matplotlib.pyplot as plt
import numpy as np
if __name__ == "__main__":
sample = np.random.uniform(-1,1,100)
freq, bins = np.histogram(sample, bins=np.arange(-1,1,0.05) )
plt.figure()
plt.hist(freq, range=bins)
plt.show()
【问题讨论】:
-
请在此处包含最小示例,该 pastebin 链接可能会过期。
-
这应该做什么?您没有传递频率,而是将样本传递给
plt.hist。 -
您可以通过编写自己的答案来自己回答问题,以便将其标记为“已回答”,或者您可以删除问题。但是,已删除的问题无法支持其他人...
标签: python matplotlib