Python basic tutorial notes-Project 2-good painting-Day1, python-day1
Today, let's start with Project 2: drawing a good image.
The graphical generation package ReportLab is used in the project. Therefore, install the package easy_install reportlab first.
As you can see from the book, this package is mainly used for plotting.
Such as writing and drawing. Run the Code:
from reportlab.lib import colorsfrom reportlab.graphics.shapes import Drawing,String,PolyLinefrom reportlab.graphics import renderPDFd = Drawing(100,100)d.add(String(50,50,'Hello world',textAnchor = 'middle'))d.add(PolyLine([(0,0),(10,0),(10,10),(0,10)],strokeColor=colors.blue))renderPDF.drawToFile(d,'h.pdf','A TEST')
You can get a PDF file named h under the current directory. The file content is:
From the code, we can see that d is a 100*100 canvas. By adding the variables you want to draw to the canvas, You can execute the renderPDF. drawToFile function to display d as a PDF file.
Finally, the book draws a preliminary sunspot line. The Code is as follows:
from reportlab.lib import colorsfrom reportlab.graphics.shapes import Drawing,String,PolyLinefrom reportlab.graphics import renderPDFdata = [# Year Month Predicted High Low(2007, 8, 113.2, 114.2, 112.2),(2007, 9, 112.8, 115.8, 109.8),(2007, 10, 111.0, 116.0, 106.0),(2007, 11, 109.8, 116.8, 102.8),(2007, 12, 107.3, 115.3, 99.3),(2008, 1, 105.2, 114.2, 96.2),(2008, 2, 104.1, 114.1, 94.1),(2008, 3, 99.9, 110.9, 88.9),(2008, 4, 94.8, 106.8, 82.8),(2008, 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 = [((row[0] + row[1]/12.0) - 2007)*200 - 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,'hello.pdf','A Simple PDF file')
Execution result:
In this Code, there are two points at the beginning:
1.zip Functions
2. Values of pred, high, and low
Let's take a look at the value assignment first. Pred = [row [2]-40 for row in data], so we can generate a list?
Try the Code:
l = [(1,2),(3,4),(5,6),(7,8)]b = [r[1] for r in l]print b
That is to say, add the r [1] of each element r in l to B. Execution result:
The result shows that it is similar.
Next, let's look at the zip function.
If the zip function is found online, the system returns a tuple list by accepting any number of (including 0 and 1) sequences as parameters.
Test the Code directly:
That is to say, zip splits the nth parameter 1, 2 ,... The r elements constitute the r parameters N1, N2 ,... Nn tuple.
As to why times should be assigned [(row [0] + row [1]/12.0)-2007) * 200-110 for row in data], this is because you need to place the graph in a proper position in the file.
After trying to assign the value of times to times = [(row [0] + row [1]/12.0)-2007) for row in data], the effect is:
This is today.