如何将两个数据集绘制为使用相同大小的容器?

时间:2019-05-20 12:47:11

标签: matplotlib

我有两个不同的数据集。我想使用两个不同的数据集绘制直方图,但保持bin相同,每个bin的宽度和范围应该相同。

org.xml.sax.SAXParseException: The reference to entity "zookeeperHost" must end with the ';' delimiter.

2 个答案:

答案 0 :(得分:2)

您可以查看matplotlib.pyplot.hist的文档,您会发现bins参数可以是整数(定义垃圾箱的数量)或序列(定义垃圾箱本身的边缘) 。

因此,您需要手动定义要使用的垃圾箱,并将其传递给plt.hist

import matplotlib.pyplot as plt
import numpy as np

bin_edges = [0, 2, 4, 6, 8]
data = np.random.rand(50) * 8 

plt.hist(data, bins=bin_edges)

答案 1 :(得分:1)

You can pass the bins returned from your first histogram plot as an argument to the second histogram to make sure both have the same bin sizes.


Complete answer:

import numpy as np
import matplotlib.pyplot as plt

Data1 = np.array([1, 2, 3, 3, 5, 6, 7, 8])
Data2 = np.array([1, 2, 3, 4, 6, 7, 8, 8])
n, bins, patches = plt.hist(Data1, bins=20, label='Data 1')
plt.hist(Data2, bins=bins, label='Data 2')
plt.ylabel("no of states")
plt.xlabel("bins")
plt.legend()
plt.show()

enter image description here