Creating Plots for Inclusion in ReportLab PDF

I'm hoping that someone's already addressed the need I have so I don't
need to re-invent this wheel. But, if no one has a ready-made solution, I
still need some expert advice in how to build this wheel myself.

   My python application uses the wxPython widget library, SQLite (using the
pysqlite2 library), ReportLab for the reports, and matplotlib for data
plotting. Right now my attention is on writing the function(s) that plot
multiple curves on a common axes set for inclusion in a PDF report. I've
been struggling with this for several days without tripping over the
solution.

   Here's what I'm trying to do, using a town for an analogy. The data on
towns, streets, and houses are stored in database tables. What I want are
plots of all house shapes on each street in each town, in .png format for
inclusion in the ReportLab output. There can be from 1 to 7 houses per
street, and the shapes are defined as distribution curves (e.g., Bell, S-,
Z-, trapezoidal curves).

   In pseudocode, this is what I have:
   for each town:
     for each street:
       hold(True)
       if shape == 'first shape':
         call functions.firstShape()
             if shape == 'second shape':
               call functions.secondShape()
     etc.
             hold()

   This does not work. First, 'hold()' is not known in this module, but all
appropriate matplotlib components are imported. Second, I must not have the
functions correctly written; for example, this error:

   File "/data1/eikos/reports.py", line 421, in inputVals
     functions.leftShoulderCurve(self.row[10],self.row[11],self.row[9])
   File "/data1/eikos/functions.py", line 542, in leftShoulderCurve
     p.plot(x, y, color='red', lw=2)
NameError: global name 'p' is not defined

   And here is functions.leftShoulderCurve:

def leftShoulderCurve(hl, hr, lr):
   hiLeft = hl
   hiRight = hr
   lowRight = lr
   x, y = zip(*[(hiLeft, 1.0), (hiRight, 1.0), (lowRight, 0.0)])
   p.plot(x, y, color='red', lw=2)
   p.axis([0, 100, 0.0, 1.0])
   p.xlabel('Universe of Discourse')
   p.ylabel('Membership Grade')
   p.show()

   If I add instance reference of 'self' prepended to 'p.', that throws the
same error.

   What I need is guidance on where to start to be able to produce these
plots. (Display of individual curves will be addressed after I get the
report completed.)

TIA,

Rich

···

--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863

Rich Shepard wrote:


   I'm hoping that someone's already addressed the need I have so I don't
need to re-invent this wheel. But, if no one has a ready-made solution, I
still need some expert advice in how to build this wheel myself.
My python application uses the wxPython widget library, SQLite (using the
pysqlite2 library), ReportLab for the reports, and matplotlib for data
plotting. Right now my attention is on writing the function(s) that plot
multiple curves on a common axes set for inclusion in a PDF report. I've
been struggling with this for several days without tripping over the
solution.
Here's what I'm trying to do, using a town for an analogy. The data on
towns, streets, and houses are stored in database tables. What I want are
plots of all house shapes on each street in each town, in .png format for
inclusion in the ReportLab output. There can be from 1 to 7 houses per
street, and the shapes are defined as distribution curves (e.g., Bell, S-,
Z-, trapezoidal curves).
In pseudocode, this is what I have:
for each town:
for each street:
hold(True)
if shape == 'first shape':
call functions.firstShape()
if shape == 'second shape':
call functions.secondShape()
etc.
hold()
This does not work. First, 'hold()' is not known in this module, but all
appropriate matplotlib components are imported. Second, I must not have the
functions correctly written; for example, this error:
File "/data1/eikos/reports.py", line 421, in inputVals
functions.leftShoulderCurve(self.row[10],self.row[11],self.row[9])
File "/data1/eikos/functions.py", line 542, in leftShoulderCurve
p.plot(x, y, color='red', lw=2)
NameError: global name 'p' is not defined

I’m guessing that you do not have the following statement in your
module:

  • import pylab as p
    which is required if you are going to use statements like:
  • p.plot(…)
  • p.axis(…)
  • and so on.
    Of course, you could simply do the following:
  • import pylab
    and then use statements like:
  • pylab.plot(…)
  • and so on
···

And here is functions.leftShoulderCurve:
def leftShoulderCurve(hl, hr, lr):
hiLeft = hl
hiRight = hr
lowRight = lr
x, y = zip(*[(hiLeft, 1.0), (hiRight, 1.0), (lowRight, 0.0)])
p.plot(x, y, color='red', lw=2)
p.axis([0, 100, 0.0, 1.0])
p.xlabel('Universe of Discourse')
p.ylabel('Membership Grade')
p.show()
If I add instance reference of 'self' prepended to 'p.', that throws the
same error.
What I need is guidance on where to start to be able to produce these
plots. (Display of individual curves will be addressed after I get the
report completed.)
TIA,
Rich

I'm guessing that you do not have the following statement in your module:

  * *import pylab as p*

which is required if you are going to use statements like:

  * *p.plot(...)*
  * *p.axis(...)*
  * *and so on.*

   Good catch, Jim! This is the format I was using while I had ...

Of course, you could simply do the following:

  * *import pylab*

   ... used this form for the import statement.

   Now, it's sorta' working. That is, I get all plots on one set of axes, not
separated by 'street' or 'town', and it displays in a popped up window.
Leaves the application hanging in an infinite loop, too.

   So, now that output is produced, I can start work on getting it correct.

Many thanks,

Rich

···

On Fri, 18 Jan 2008, Jim Vickroy wrote:

--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863