Python Basic Tutorial Notes-Item 2-format good picture-day1

Source: Internet
Author: User
Tags bit set polyline

Start today to see item two: Frame good painting

The project will use graphics to generate the package Reportlab, so first install this package:easy_install Reportlab

As you can see from the book, this bag is mainly used for drawing.

such as writing, drawing lines and so on. Execute 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 ')

A pdf file named H is available under the current directory file with the following contents:


From the code, you can see that D is a 100*100 Canvas, by adding the variables you want to draw on the canvas, and finally executing the renderpdf.drawtofile function D displayed as PDF file.

A preliminary sunspot line was drawn in the final book . The code is as follows:

From reportlab.lib import colorsfrom reportlab.graphics.shapes import Drawing,string,polylinefrom reportlab.graphics     Import renderpdfdata = [# Year of 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, 1   15.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), (4, 94.8, 106.8, 82.8), (5, 91.2, 104.2, 78.2),]drawing = Drawing (200,150) pred = [r ow[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)-*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 effect:

There are two places in this code that are not understood at first:

1.zip function

Assignment of 2.pred,high and Low

Look at the assignment first. pred = [row[2]-40 for row in data] , the feeling is that it can generate a List ?

The code tries:

L = [(n), (3,4), (5,6), (7,8)]b = [r[1] for R in L]print b

That is, the r[1 of each element r in L is added to b . Execution Result:


It turns out that's almost what it means.

Next look at the zip function

The function of the zip function on the Internet is to accept any number of sequences (including 0 and 1 ) as parameters, returning a Tuple list.

Under direct code test:


That is, zip will be N parameters of 1,2, ... R elements are composed of R parameters of N1,N2, ... The tuple of the Nn.

As for why the Times should be assigned to [(((row[0] + row[1]/12.0)-*200-110 for row in data], is to put the diagram in the file in the appropriate bit Set up.

Try to assign times = [((row[0] + row[1]/12.0)-for row in data], and the effect is:



We'll be here today.


Python Basic Tutorial Notes-Item 2-format good picture-day1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.