[Tutor] pdf generation problem

Jason Snyder jmssnyder at ucdavis.edu
Fri Mar 3 19:28:36 EST 2017


I have the following program where I am trying to generate a pdf:

      1 import matplotlib
      2 matplotlib.use('AGG')
      3 import matplotlib.pyplot as plt
      4 import matplotlib.image as image
      5 import matplotlib.gridspec as gridspec
      6 from matplotlib.backends.backend_pdf import PdfPages
      7 import numpy as np
      8
      9 np.random.seed(0)
     10
     11 x, y = np.random.randn(2, 100)
     12
     13 with PdfPages('wx_plot.pdf') as pdf:
     14   fig, (ax1,ax2) = plt.subplots(nrows=2, figsize=(8,11))
     15   gs = gridspec.GridSpec(2, 1,
     16                     height_ratios=[1.5,3]
     17                     )
     18   ax1 = plt.subplot(gs[0])
     19   ax1.xcorr(x, y, usevlines=True, maxlags=50, normed=True, lw=2)
     20   ax1.grid(True)
     21   ax1.axhline(0, color='black', lw=2)
     22
     23   ax2 = plt.subplot(gs[1])
     24   ax2.acorr(x, usevlines=True, normed=True, maxlags=50, lw=2)
     25   ax2.grid(True)
     26   ax2.axhline(0, color='black', lw=2)
     27   pdf.savefig('fig')
     28 pdf.close()

When I run it I get the following error:

Traceback (most recent call last):
  File "testinger.py", line 13, in <module>
    with PdfPages('wx_plot.pdf') as pdf:
AttributeError: __exit__


What is going on here and how do I resolve this issue?


More information about the Tutor mailing list