【问题标题】:matplotlib hist gives me "too many values to unpack"matplotlib hist 给了我“太多无法解包的值”
【发布时间】: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


【解决方案1】:

不需要使用numpy对数据进行预处理,只需将数据直接传递给matplotlib的hist函数即可:

  sample =  np.random.uniform(-1,1,10000)
  #freq, bins = np.histogram(sample, bins=np.arange(-1,1,0.05) )

  plt.figure()
  plt.hist(sample, bins=100)
  plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-13
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 2021-02-10
    • 2017-08-09
    相关资源
    最近更新 更多