使用PyLab在图中放置多个直方图

时间:2014-02-04 21:52:59

标签: python matplotlib

我的功能如下:

def drawChart(data,title):
    P.title(title)
    P.hist(data, bins=20, histtype='stepfilled')
    P.xlabel("Relevance")
    P.ylabel("Frequency")
    P.savefig(title + '.pdf')

这会创建直方图的pdf。但是,我对此进行了大约6次调用,理想情况下将它们全部保存为一个文档。

现在首先如何整理它们并从drawChart中返回一个对象以实现此目的?

我见过人们使用数字here

1 个答案:

答案 0 :(得分:2)

所以你想要subplots。可能的示例如下:

import numpy as np
import matplotlib.pyplot as plt
# create some data
data = np.random.rand(6,10)
fig, ax = plt.subplots(3,2)
ax = ax.reshape(6)
for ind, d in enumerate(data):
  ax[ind].hist(d)
fig.tight_layout()
plt.show()

给出了像
这样的数字 enter image description here

可以在matplotlib gallery中找到更多子图的示例,例如here