Matplotlib has a lot of overhead for creation of the figure, etc. even before saving it to pdf. So if your plots are similar you can safe a lot of "setting up" by reusing elements, just like you will find in animation examples for matplotlib.
You can reuse the figure and axes in this example:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
X = range(10)
Y = [ x**2 for x in X ]
fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111)
for n in range(100):
ax.clear() # or even better just line.remove()
# but should interfere with autoscaling see also below about that
line = ax.plot(X, Y)[0]
fig.savefig("test.pdf")
Note that this does not help that much. You can save quite a bit more, by reusing the lines:
line = ax.plot(X, Y)[0]
for n in range(100):
# Now instead of plotting, we update the current line:
line.set_xdata(X)
line.set_ydata(Y)
# If autoscaling is necessary:
ax.relim()
ax.autoscale()
fig.savefig("test.pdf")
This is close to twice as fast as the initial example for me. This is only an option if you do similar plots, but if they are very similar, it can speed up things a lot. The matplotlib animation examples may have inspiration for this kind of optimization.