Python Simple Drawing a __python

Source: Internet
Author: User

I've always wanted to learn python, but programmers know that when you actually use a language, it's the most efficient, so I'm going to use it now.

Originally this drawing work, my colleagues have done with MATLAB, but I have always felt that the MATLAB does not catch a cold, so try to use Python to do.


Example: http://matplotlib.org/examples/index.html

First of all in the Ubuntu16.04 system with Python (how convenient how to come)


Need to install PIP


Install dependent libraries




OK, start practicing.


1. Draw a straight line

Import NumPy as NP
import Matplotlib.pyplot as Plt

x=[0,1]
y=[0,1
plt.figure ()
Plt.plot (x,y)
plt.show ()
#plt. Savefig ("Easyplot.jpg")


2. Draw a Circle

From matplotlib.patches import Ellipse, Circle

import matplotlib.pyplot as plt
fig = plt.figure ()
ax = fig.ad D_subplot (x)
cir1 = Circle (xy = (0.0, 1.0), radius=3, alpha=0.4) 
Ax.add_patch (CIR1)
plt.axis (' scaled ')
plt.axis (' equal ') 
plt.title (' Circle ')
plt.show ()


3. Draw Scatter plot (this is my main use at present)

from numpy import *;
Import NumPy as NP
import Matplotlib.pyplot as plt

N = 
x = Np.random.rand (n) 
y = Np.random.rand (n) C14/>colors = Np.random.rand (n) Area 
= Np.pi * Np.random.rand (n) **2  
plt.scatter (x, Y, S=area, C=colors, a lpha=0.5, marker= (9, 3))
plt.show ()

Here we use a function to draw scatter graphs in a Matplotlib.pyplot sub library.
Matplotlib.pyplot.scatter (x, Y, s=20, C=none, marker= ' O '),
Cmap=none, Norm=none, Vmin=none, Vmax=none, Alpha=none,
Linewidths=none, Verts=none, Edgecolors=none, Hold=none,
Data=none, **kwargs)
This function receives a lot of parameters, there is a default value usually do not need us to specify, is optional, this time we used in addition to the basic x, y parameters, and C,s,alpha and Marker,c is the point of the specified color array, s is the point of the area size, Alpha is the point of the color transparency, Marker is the shape of a fixed-point marker. In the example specified transparency for 0.5,c and S is randomly generated, we want to change the value of marker, marker have a number of values to choose from, the following table shows the example code based on the change marker value after the effect:

Result
Marker
”.”
”,”
"O"
"V"
^
"<"
">"
"1"
"2"
"3"
"4"
"8"
"S"
"P"
“*”
"H"
H
+
"X"
D
"D"
“ ”
“_”
"None" Yes, there's nothing.
"$...$"
(NumSides, style, angle)
Eg: (9,0, 30)
Note: NumSides is the number of edges,
Angle is the angle of rotation,
Style only 0,1,2,3 four values
(NumSides, style, angle)
Eg: (9,1, 30)
(NumSides, style, angle)
Eg: (9,2, 30)
(NumSides, style, angle)
Eg: (9,3, 30)
Note: The values of numsides and angle are automatically ignored at this time


4. Various shapes

Import Matplotlib.pyplot as Plt plt.rcdefaults () import NumPy as NP import Matplotlib.pyplot as PLT import Matplotlib.pat H as MPath import matplotlib.lines as Mlines import matplotlib.patches as mpatches from matplotlib.collections import PATC Hcollection def label (XY, text): y = xy[1]-0.15 # shift Y-value for label so that it ' s below the artist plt.t Ext (xy[0], y, text, ha= "center", family= ' Sans-serif ', size=14) FIG, ax = plt.subplots () # Create 3x3 grid to plot the AR tists Grid = np.mgrid[0.2:0.8:3j, 0.2:0.8:3j].reshape (2,-1). T patches = [] # Add a circle circle = mpatches. Circle (Grid[0], 0.1, ec= "None") Patches.append (Circle) label (grid[0), "Circle") # Add a rectangle rect = mpatches. Rectangle (Grid[1]-[0.025, 0.05], 0.05, 0.1, ec= "None") patches.append (Rect) label (Grid[1], "Rectangle") # Add a wedge W Edge = Mpatches. Wedge (grid[2], 0.1, 270, ec= "None") patches.append (wedge) label (grid[2), "wedge") # Add a Polygon Polygon = mpatches. Regularpolygon (Grid[3], 5, 0.1) Patches.append (polygon) label (Grid[3], "polygon") # add an ellipse ellipse = mpatches. Ellipse (Grid[4], 0.2, 0.1) patches.append (Ellipse) label (GRID[4), "Ellipse") # Add an arrow arrow = Mpatches. Arrow (grid[5, 0]-0.05, grid[5, 1]-0.05, 0.1, 0.1, width=0.1) Patches.append (arrow) label (GRID[5], "Arrow") # Add a PA th patch Path = MPath. Path path_data = [(Path.moveto, [0.018, -0.11]), (Path.curve4, [ -0.031, -0.051]), (Path.curve4, [-0.115, 0.0 ), (Path.curve4, [ -0.03, 0.073]), (Path.lineto, [ -0.011, 0.039]), (Path.curve4, [0.043, 0.121]), (P Ath. CURVE4, [0.075, -0.005]), (Path.curve4, [0.035, -0.027]), (Path.closepoly, [0.018, -0.11])] codes, verts = Zi P (*path_data) path = MPath. Path (verts + grid[6], codes) patch = Mpatches. Pathpatch (path) patches.append (patch) label (Grid[6], "Pathpatch") # Add a fancy box FancyBox = mpatches. Fancybboxpatch (Grid[7]-[0.025, 0.05], 0.05, 0.1, boxstyle=mpatches. Boxstyle ("Round", pad=0.02)) Patches.append (fancybox) label (Grid[7], "Fancybboxpatch") # Add a line x, y = Np.array ([[-0.06, 0.0, 0.1], [0.05,-0.05 , 0.05]] line = Mlines. Line2D (x + grid[8, 0], y + grid[8, 1], lw=5., alpha=0.3) label (GRID[8), "line2d") colors = Np.linspace (0, 1, len (patches) ) collection = Patchcollection (patches, CMAP=PLT.CM.HSV, alpha=0.3) Collection.set_array (Np.array (colors)) ax.add_
Collection (collection) Ax.add_line (line) plt.subplots_adjust (left=0, Right=1, bottom=0, top=1) plt.axis (' equal ') Plt.axis (' Off ') plt.show ()


Based on the above example, most of the comparison of the basic drawing should be completed, the program Ape is best at suit, extrapolate


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.