Python ing instance tutorial, python ing instance tutorial

Source: Internet
Author: User
Tags sca

Python ing instance tutorial, python ing instance tutorial

This article introduces how to use the python turtle module, that is, the turtle plot, in the form of examples. For those who need to program graphics, I believe it will be of some reference value.

Introduction to the python turtle module:
A simple plotting tool introduced in python2.6 called Turtle Graphics)

1. To use a turtle plot, first we need to import turtle, As shown below:

From turtle import * # import all the methods in turtle

2. Turtle plot attributes:

(1)Location
(2)Direction
(3)Paint Brush(Attributes of the paint brush, color, and width of the draw line)

3. There are many commands to manipulate turtle plotting. These commands can be divided into two types: Motion commands and brush control commands.

(1)Motion command:

Forward (degree) # moving forward distance degree represents the distance from backward (degree) # moving backward distance degree represents the distance from right (degree) # How much left (degree) to the right) # How many degrees to move to the left goto (x, y) # Move the paint brush to the position of x, y stamp () # copy the current graphic speed (speed) # The paint speed range of the paint brush is an integer of [0, 10 ].

(2)Paint Brush control command:

Down () # Draw a chart when you move it. If you do not move it, you can draw up () when you move it. # When you move it, do not draw a graph. pensize (width) # When you draw a graph, color (colorstring) # fillcolor (colorstring) when drawing a drawing # fill color of the drawing fill (Ture) fill (false)

4. For introduction to turtle, let's take a look at the following examples:

(1) draw a square:

Import turtle import time # define the color of the paint brush when drawing turtle. color ("purple") # define the width of the paint brush line when drawing turtle. size (5) # define the drawing speed turtle. speed (10) # Draw turtle starting from 0 to 0. goto () # Draw the four sides of a square for I in range (4): turtle. forward (100) turtle. right (90) # When the paint brush moves to the point (-150,-120), the turtle is not drawn. up () turtle. goto (-150,-120) # redefine the paint brush color turtle. color ("red") # print "Done" turtle on (-150,-120. write ("Done") time. sleep (3)

(2) Draw a star:

import turtleimport timeturtle.color("purple")turtle.pensize(5)turtle.goto(0,0)turtle.speed(10)for i in range(6): turtle.forward(100) turtle.right(144)turtle.up()turtle.forward(100)turtle.goto(-150,-120)turtle.color("red")turtle.write("Done")time.sleep(3)

Two simple examples are provided here. You can draw more complex images based on the above ideas and methods.


Python drawing storage (savefig)

We recommend that you use an object-oriented method to draw a picture. An image is used as an object, so that you can only save the current image each time you call the Save image method of an object. In this way, the third graph has three subgraphs because all three subgraphs are painted on the same object. To wrap the object-oriented drawing library into a function-only calling interface, the pyplot module stores information such as the current chart and the current subgraph. The Current charts and subgraphs can be obtained using gcf () and gca (), which start with "Get Current Figure" and "Get Current Axis. Gcf () obtains the Figure object that represents the chart, while gca () obtains the Axes object that represents the subgraph. Next we will run the "matplotlib_simple_plot.py" program in the previous section in IPython, and then call gcf () and gca () to view the current Figure and Axes objects. Here is an example of import numpy as npimport matplotlib. pyplot as pltplt. figure (1) # create a chart 1plt. figure (2) # create a chart 2ax1 = plt. subplot (211) # create a subgraph 1ax2 = plt in Chart 2. subplot (212) # create a subgraph 2x = np in Chart 2. linspace (0, 3,100) for I in xrange (5): plt. figure (1) rows # select Chart 1 plt. plot (x, np. exp (I * x/3) plt. sca (ax1) Partition # select the subgraph 1 plt of chart 2. plot (x, np. sin (I * x) plt. sca (ax2) # select the subgraph 2 plt of chart 2. plot (x, np. cos (I * x) plt. show ()

Python plotting Problems


Draw the triangle's angle bisector, height, and midline in Word:
1. Execute the "View/toolbar/drawing" command to bring up the "Drawing" toolbar;
2. Click "Drawing/drawing grid" on the "Drawing" toolbar to open the "Drawing grid" dialog box;
3. in the dialog box, Set grid, horizontal spacing, 0.86 rows, Vertical spacing, and 0.6 rows ", select "display gridlines on the screen" "vertical interval" "2" and "horizontal interval" "2 ";
4. Click OK to display the drawing gridlines in the document;
5. Draw the angular bisector, height, and midline of the triangle Based on the gridlines (draw the line using the "Straight Line" tool on the "Drawing" toolbar );
6. select the triangle, angular bisector, height, and midline simultaneously, right-click, and run the "Combination/Combination" shortcut menu command to combine the drawn images, easy to move the entire image.
 

Related Article

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.