【问题标题】:Plot histogram of a large number of integers using matplotlib使用 matplotlib 绘制大量整数的直方图
【发布时间】:2014-10-02 08:23:45
【问题描述】:

我试图在 large 范围内的 large 数字列表中绘制整数的频率。更具体地说:

ints = np.random.random_integers(0,1440,15000)

ints 是一个长长的整数列表,其值介于 0 和 1440 之间。接下来我想绘制一个直方图来可视化频率。为此,我使用了类似的东西:

fig_per_hour = plt.figure()
per_hour = fig_per_hour.add_subplot(111)
counts, bins, patches = per_hour.hist(
    ints, bins = np.arange(0,1441), normed = False, color = 'g',linewidth=0)
plt.show()

但我面临两个问题:

  1. x 轴右端的一个恼人的间隙。虽然我指定了正确(?)数量的垃圾箱,但该图并未反映它。
  2. 由于整数的采样范围很大,每个条形都很薄,很难区分。是否可以拉伸 x 轴以使条形变宽?原因是我想注释 x 轴和一些条形。

作为参考,这是我到目前为止的输出:

【问题讨论】:

  • 您可以指定 plt.xlimits(min,max) 以确保 x 轴范围在您的范围内。和plt.hist(data, bins = range(min,max+binwidth,binwidth)) 以确保垃圾箱平均分布

标签: python matplotlib plot


【解决方案1】:

我会使用 set_xlim 和较少数量的垃圾箱,例如bins = 100:

from matplotlib import pyplot as plt
import numpy as np

ints = np.random.random_integers(0,1440,15000)

fig_per_hour = plt.figure()
per_hour = fig_per_hour.add_subplot(111)
counts, bins, patches = per_hour.hist(
    ints, bins = 100, normed = False, color = 'g',linewidth=0)
plt.gca().set_xlim(ints.min(), ints.max())
plt.show()


编辑:您可以手动调整窗口大小:

原则上,您可以使用plt.figure(figsize=(20, 2)) 以编程方式执行此操作。但不知何故,窗口大小受限于屏幕大小。

【讨论】:

  • 这很有帮助,但我仍然希望有足够的 (=1440) 个垃圾箱。我对 24 小时内的“每分钟”行为感兴趣。我不介意生成的图像的宽度会很大。
  • @Dror:我并没有真正得到应用程序。你不能手动调整图形窗口的大小吗?您还可以缩放和平移轴。还是您需要以编程方式进行?
  • 程序化解决方案最适合我,因为我想生成将自动集成到文档中的图像。
猜你喜欢
  • 2016-06-09
  • 2015-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-05
相关资源
最近更新 更多