Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 21 ■ PROJECT 2: PAINTING A PRETTY PICTURE 415 Constructing Some PolyLines To create a line diagram (a graph) of the sunspot data, you have to create some lines. In fact, you have to create several lines that are linked. ReportLab has a special class for this: PolyLine. A PolyLine is created with a list of coordinates as its first argument. This list is of the form [(x0, y0), (x1, y1), ...], with each pair of x and y coordinates making one point on the PolyLine. See Figure 21-2 for a simple PolyLine. Figure 21-2. PolyLine([(0, 0), (10, 0), (10, 10), (0, 10)]) To make a line diagram, one polyline must be created for each column in the data set. Each point in these polylines will consist of a time (constructed from the year and month) and a value (which is the number of sunspots taken from the relevant column). To get one of the columns (the values), list comprehensions can be useful: pred = [row[2] for row in data] Here pred (for “predicted”) will be a list of all the values in the third column of the data. You can use a similar strategy for the other columns. (The time for each row would have to be calculated from both the year and month: for example, year + month/12.) Once you have the values and the time stamps, you can add your polylines to the drawing like this: drawing.add(PolyLine(zip(times, pred), strokeColor=colors.blue)) It isn’t necessary to set the stroke color, of course, but it makes it easier to tell the lines apart. (Note how zip is used to combine the times and values into a list of tuples.)
416 CHAPTER 21 ■ PROJECT 2: PAINTING A PRETTY PICTURE The Prototype You now have what you need to write your first version of the program. The source code is shown in Listing 21-2. Listing 21-2. The First Prototype for the Sunspot Graph Program from reportlab.lib import colors from reportlab.graphics.shapes import * from reportlab.graphics import renderPDF data = [ # Year Month Predicted High Low (2005, 8, 113.2, 114.2, 112.2), (2005, 9, 112.8, 115.8, 109.8), (2005, 10, 111.0, 116.0, 106.0), (2005, 11, 109.8, 116.8, 102.8), (2005, 12, 107.3, 115.3, 99.3), (2006, 1, 105.2, 114.2, 96.2), (2006, 2, 104.1, 114.1, 94.1), (2006, 3, 99.9, 110.9, 88.9), (2006, 4, 94.8, 106.8, 82.8), (2006, 5, 91.2, 104.2, 78.2), ] drawing = Drawing(200, 150) pred = [row[2]-40 for row in data] high = [row[3]-40 for row in data] low = [row[4]-40 for row in data] times = [200*((row[0] + row[1]/12.0) - 2005)-110 for row in data] drawing.add(PolyLine(zip(times, pred), strokeColor=colors.blue)) drawing.add(PolyLine(zip(times, high), strokeColor=colors.red)) drawing.add(PolyLine(zip(times, low), strokeColor=colors.green)) drawing.add(String(65, 115, 'Sunspots', fontSize=18, fillColor=colors.red)) renderPDF.drawToFile(drawing, 'report1.pdf', 'Sunspots') As you can see, I have adjusted the values and time stamps to get the positioning right. The resulting drawing is shown in Figure 21-3.
- Page 395 and 396: 364 CHAPTER 17 ■ EXTENDING PYTHON
- Page 397 and 398: 366 CHAPTER 17 ■ EXTENDING PYTHON
- Page 399 and 400: 368 CHAPTER 17 ■ EXTENDING PYTHON
- Page 401 and 402: 370 CHAPTER 17 ■ EXTENDING PYTHON
- Page 404 and 405: CHAPTER 18 ■ ■ ■ Packaging Yo
- Page 406 and 407: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 408 and 409: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 410 and 411: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 412 and 413: CHAPTER 19 ■ ■ ■ Playful Prog
- Page 414 and 415: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 416 and 417: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 418 and 419: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 420: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 423 and 424: 392 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 425 and 426: 394 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 427 and 428: 396 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 429 and 430: 398 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 431 and 432: 400 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 433 and 434: 402 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 435 and 436: 404 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 437 and 438: 406 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 439 and 440: 408 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 442 and 443: CHAPTER 21 ■ ■ ■ Project 2: P
- Page 444 and 445: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 448 and 449: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 450 and 451: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 452 and 453: CHAPTER 22 ■ ■ ■ Project 3: X
- Page 454 and 455: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 456 and 457: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 458 and 459: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 460 and 461: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 462 and 463: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 464 and 465: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 466 and 467: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 468: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 471 and 472: 440 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 473 and 474: 442 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 475 and 476: 444 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 477 and 478: 446 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 479 and 480: 448 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 481 and 482: 450 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 483 and 484: 452 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 486 and 487: CHAPTER 24 ■ ■ ■ Project 5: A
- Page 488 and 489: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 490 and 491: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 492 and 493: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 494 and 495: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
416 CHAPTER 21 ■ PROJECT 2: PAINTING A PRETTY PICTURE<br />
The Pro<strong>to</strong>type<br />
You now have what you need <strong>to</strong> write your first version of the program. The source code is<br />
shown in Listing 21-2.<br />
Listing 21-2. The First Pro<strong>to</strong>type for the Sunspot Graph Program<br />
from reportlab.lib import colors<br />
from reportlab.graphics.shapes import *<br />
from reportlab.graphics import renderPDF<br />
data = [<br />
# Year Month Predicted High Low<br />
(2005, 8, 113.2, 114.2, 112.2),<br />
(2005, 9, 112.8, 115.8, 109.8),<br />
(2005, 10, 111.0, 116.0, 106.0),<br />
(2005, 11, 109.8, 116.8, 102.8),<br />
(2005, 12, 107.3, 115.3, 99.3),<br />
(2006, 1, 105.2, 114.2, 96.2),<br />
(2006, 2, 104.1, 114.1, 94.1),<br />
(2006, 3, 99.9, 110.9, 88.9),<br />
(2006, 4, 94.8, 106.8, 82.8),<br />
(2006, 5, 91.2, 104.2, 78.2),<br />
]<br />
drawing = Drawing(200, 150)<br />
pred = [row[2]-40 for row in data]<br />
high = [row[3]-40 for row in data]<br />
low = [row[4]-40 for row in data]<br />
times = [200*((row[0] + row[1]/12.0) - 2005)-110 for row in data]<br />
drawing.add(PolyLine(zip(times, pred), strokeColor=colors.blue))<br />
drawing.add(PolyLine(zip(times, high), strokeColor=colors.red))<br />
drawing.add(PolyLine(zip(times, low), strokeColor=colors.green))<br />
drawing.add(String(65, 115, 'Sunspots', fontSize=18, fillColor=colors.red))<br />
renderPDF.drawToFile(drawing, 'report1.pdf', 'Sunspots')<br />
As you can see, I have adjusted the values and time stamps <strong>to</strong> get the positioning right. The<br />
resulting drawing is shown in Figure 21-3.