Matplotlib xticks没有排列直方图

Pay*_*ian 42 python matplotlib

我正在用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()
Run Code Online (Sandbox Code Playgroud)

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

测试

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

Joe*_*ton 90

简短回答:plt.hist(data, bins=range(50))改为使用左对齐的箱子,plt.hist(data, bins=np.arange(50)-0.5)以获得中心对齐的箱子等.

此外,如果性能很重要,因为你需要唯一整数的计数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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

正如您所注意到的,这些箱子没有与整数间隔对齐.这基本上是因为你要求10 到9 之间的 10个箱子,这与要求10个唯一值的箱子不完全相同.

您想要的箱数与唯一值的数量不完全相同.在这种情况下,您实际应该做的是手动指定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.  ]))
Run Code Online (Sandbox Code Playgroud)

返回的第一个数组是计数,第二个是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.]))
Run Code Online (Sandbox Code Playgroud)

请注意,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.
Run Code Online (Sandbox Code Playgroud)

因此,您实际应该做的是准确指定您想要的bin边缘,并且包括超出最后一个数据点的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]))
Run Code Online (Sandbox Code Playgroud)

Bin Alignment


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

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

好,太棒了!但是,我们现在实际上有左对齐的箱子.如果我们希望中心对齐的箱子更好地反映这些是独特价值的事实呢?

快速的方法是只移动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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

类似地,对于右对齐的箱,只需转换-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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这种方法有两大优点.一个是速度. 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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Bin*_*nod 5

您要寻找的是了解每个垃圾箱的边缘并将其用作 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)
Run Code Online (Sandbox Code Playgroud)

现在让我们创建直方图。

n, bins, edges = plt.hist(x,bins=5,ec="red",alpha=0.7)
Run Code Online (Sandbox Code Playgroud)
  • n 是带有编号的数组。每个垃圾箱中的物品数量
  • bins 是包含 bin 边缘值的数组
  • Edges 是 patch 对象的列表

现在,既然您已经知道了从左到右开始的 bin 边缘的位置,请将其显示为 xticks。

plt.xticks(bins)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述