【问题标题】:Matplotlib xticks not lining up with histogramMatplotlib xticks 未与直方图对齐
【发布时间】:2015-01-20 21:06:08
【问题描述】:

我正在使用 matplotlib 生成一些直方图,但在弄清楚如何让直方图的 xticks 与条形对齐时遇到了一些麻烦。

这是我用来生成直方图的代码示例:

from matplotlib import pyplot as py

py.hist(histogram_data, 49, alpha=0.75)
py.title(column_name)
py.xticks(range(49))
py.show()

我知道histogram_data 数组中的所有值都在[0,1,...,48] 中。假设我做对了,这意味着有 49 个唯一值。我想显示每个值的直方图。这是生成的图片。

如何设置图表以使所有 xticks 都对齐到每个条的左侧、中间或右侧?

【问题讨论】:

  • 您的histogram_data 当绘制为直方图时,会为您提供一个包含 49 个等距数字的图形。 I'd like to show a histogram of each of those values 是什么意思??

标签: python matplotlib


【解决方案1】:

简答:改用plt.hist(data, bins=range(50)) 来获取左对齐的bin,使用plt.hist(data, bins=np.arange(50)-0.5) 来获取居中对齐的bin,等等。

此外,如果性能很重要,因为您需要唯一整数的计数,我将在最后展示几个稍微更有效的方法 (np.bincount)。

问题陈述


作为您所看到的独立示例,请考虑以下内容:

import matplotlib.pyplot as plt
import numpy as np

# Generate a random array of integers between 0-9
# data.min() will be 0 and data.max() will be 9 (not 10)
data = np.random.randint(0, 10, 1000)

plt.hist(data, bins=10)
plt.xticks(range(10))
plt.show()

正如您所注意到的,这些 bin 没有与整数间隔对齐。这基本上是因为您在 0 和 9 之间要求 10 个 bin,这与要求 10 个唯一值的 bin 不太一样。

您想要的 bin 数量与唯一值的数量不完全相同。在这种情况下,您实际上应该做的是手动指定 bin 边缘。

为了解释发生了什么,让我们跳过matplotlib.pyplot.hist,直接使用底层的numpy.histogram 函数。

例如,假设您有值[0, 1, 2, 3]。你的第一反应是:

In [1]: import numpy as np

In [2]: np.histogram([0, 1, 2, 3], bins=4)
Out[2]: (array([1, 1, 1, 1]), array([ 0.  ,  0.75,  1.5 ,  2.25,  3.  ]))

返回的第一个数组是计数,第二个是 bin 边缘(换句话说,条形边缘在您的绘图中的位置)。

请注意,我们得到了预期的计数,但是因为我们要求在数据的最小值和最大值之间设置 4 个 bin,所以 bin 边缘不在整数值上。

接下来,你可以试试:

In [3]: np.histogram([0, 1, 2, 3], bins=3)
Out[3]: (array([1, 1, 2]), array([ 0.,  1.,  2.,  3.]))

请注意,bin 边缘(第二个数组)是您所期望的,但计数不是。这是因为最后一个 bin 的行为与其他 bin 不同,如 numpy.histogram 的文档中所述:

Notes
-----
All but the last (righthand-most) bin is half-open.  In other words, if
`bins` is::

  [1, 2, 3, 4]

then the first bin is ``[1, 2)`` (including 1, but excluding 2) and the
second ``[2, 3)``.  The last bin, however, is ``[3, 4]``, which *includes*
4.

因此,您实际上应该做的是准确指定您想要的 bin 边缘,并在最后一个数据点之外包含一个或将 bin 边缘移动到 0.5 间隔。例如:

In [4]: np.histogram([0, 1, 2, 3], bins=range(5))
Out[4]: (array([1, 1, 1, 1]), array([0, 1, 2, 3, 4]))

Bin 对齐


现在让我们将它应用到第一个示例中,看看它是什么样子的:

import matplotlib.pyplot as plt
import numpy as np

# Generate a random array of integers between 0-9
# data.min() will be 0 and data.max() will be 9 (not 10)
data = np.random.randint(0, 10, 1000)

plt.hist(data, bins=range(11)) # <- The only difference
plt.xticks(range(10))
plt.show()

好的,太好了!但是,我们现在有效地拥有左对齐的 bin。如果我们希望中心对齐的 bin 更好地反映这些是唯一值这一事实怎么办?

快速的方法是移动 bin 边缘:

import matplotlib.pyplot as plt
import numpy as np

# Generate a random array of integers between 0-9
# data.min() will be 0 and data.max() will be 9 (not 10)
data = np.random.randint(0, 10, 1000)

bins = np.arange(11) - 0.5
plt.hist(data, bins)
plt.xticks(range(10))
plt.xlim([-1, 10])

plt.show()

对于右对齐的 bin 类似,只需移动 -1

另一种方法


对于唯一整数值​​的特殊情况,我们可以采取另一种更有效的方法。

如果您要处理从 0 开始的唯一整数计数,最好使用 numpy.bincount 而不是使用 numpy.hist

例如:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randint(0, 10, 1000)
counts = np.bincount(data)

# Switching to the OO-interface. You can do all of this with "plt" as well.
fig, ax = plt.subplots()
ax.bar(range(10), counts, width=1, align='center')
ax.set(xticks=range(10), xlim=[-1, 10])

plt.show()

这种方法有两大优势。一是速度。 numpy.histogram(因此是plt.hist)基本上通过numpy.digitize然后numpy.bincount运行数据。因为您正在处理唯一的整数值,所以无需执行 numpy.digitize 步骤。

但是,更大的优势是对显示的更多控制。如果您更喜欢较细的矩形,只需使用较小的宽度:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randint(0, 10, 1000)
counts = np.bincount(data)

# Switching to the OO-interface. You can do all of this with "plt" as well.
fig, ax = plt.subplots()
ax.bar(range(10), counts, width=0.8, align='center')
ax.set(xticks=range(10), xlim=[-1, 10])

plt.show()

【讨论】:

【解决方案2】:

您正在寻找的是了解每个 bin 的边缘并将其用作 xtick。

假设您在 x 中有一些数字来生成直方图。

import matplotlib.pyplot as plt
import numpy as np
import random
n=1000
x=np.zeros(1000)
for i in range(n):
    x[i]=random.uniform(0,100)

现在让我们创建直方图。

n, bins, edges = plt.hist(x,bins=5,ec="red",alpha=0.7)
  • n 是编号为的数组。每个 bin 中的项目数
  • bins 是值在 bin 边缘的数组
  • edges 是补丁对象列表

现在,由于您有从左到右开始的 bin 边缘的位置,因此将其显示为 xticks。

plt.xticks(bins)
plt.show()

【讨论】:

    【解决方案3】:

    如果评论bins.append(sorted(set(labels))[-1]):

    bins = [i_bin - 0.5 for i_bin in set(labels)]
    # bins.append(sorted(set(labels))[-1])
    plt.hist(labels, bins)
    plt.show()
    

    如果没有:

    bins = [i_bin - 0.5 for i_bin in set(labels)]
    bins.append(sorted(set(labels))[-1])
    plt.hist(labels, bins)
    plt.show()
    

    【讨论】:

      【解决方案4】:

      使用 OO 接口配置刻度具有使标签居中同时保留 xticks 的优点。此外,它适用于任何绘图功能,不依赖于np.bincount()ax.bar()

      import matplotlib.pyplot as plt
      import matplotlib.ticker as tkr
      data = np.random.randint(0, 10, 1000)
      mybins = range(11)
      fig, ax = plt.subplots()
      ax.hist(data, bins=mybins, rwidth=0.8)
      ax.set_xticks(mybins)
      ax.xaxis.set_minor_locator(tkr.AutoMinorLocator(n=2))
      ax.xaxis.set_minor_formatter(tkr.FixedFormatter(mybins))
      ax.xaxis.set_major_formatter(tkr.NullFormatter())
      
      for tick in ax.xaxis.get_minor_ticks():
          tick.tick1line.set_markersize(0)
      


      (来源:pbrd.co

      【讨论】:

      • NameError: name 'subplots' is not defined
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 2019-04-06
      • 2023-01-02
      相关资源
      最近更新 更多