-2

I have a short function as follows:

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

This creates a pdf of my histogram. However I make around 6 calls to this, and would ideally like to save them all as one document.

Now firstly how do I collate them all and return an object from the drawChart for this to happen?

I have seen people use figure here

Community
  • 1
  • 1
redrubia
  • 2,256
  • 6
  • 33
  • 47

1 Answers1

2

So you want subplots. A possible example could look like:

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()

which gives a figure like
enter image description here

More examples of subplots can be found in the matplotlib gallery, e.g. here.

Jakob
  • 19,815
  • 6
  • 75
  • 94