Drawing: Matplotlib

Source: Internet
Author: User

Used to draw some data graph, the classmate recommended, very useful. Very good official documents: http://matplotlib.org/contents.html

0. Installation

Can be directly pip install, there are some dependencies follow the prompts, specifically also forget.

1. Basic Drawing

Import= [1,2,3,4= [1,2,3,4]plt.plot (XS, YS) plt.show ()

XS represents the x-coordinate of the point, Ys represents the y-coordinate of the point, the point that is drawn is (Xs[i], ys[i]), and by default, the dots are connected in turn by line. You can see a popup with a straight line that has a slope of 1 at the origin point. Note that after show, you can't show again, you need to plot again. Indicates that the plot module is stateful.

2. Scatter point

You can set the drawing point mode (markers), connect the dots without using a straight line, and just draw a scatter point:

Import= [1,2,3,4= [1,2,3,4]plt.plot (XS, YS, "ob");p lt.show ()

What you see at this point is a few discrete points. Here "OB" means to use the marker of the circle and the color is blue, where filled_markers can be:

' O '-dots, ' V '-inverted triangles, ' ^ '-deltoid, ' < '-left triangle, ' > '-Right triangle, ' 8 ', ' s '-square, ' p '-convex pentagon, ' * '-pentagram, ' h ', ' h ', ' d '-rhombus, ' d '-Flat Diamond

All possible markers is defined here:

Marker Description
”.” Point
”,” Pixel
"O" Circle
"V" Triangle_down
^ Triangle_up
"<" Triangle_left
">" Triangle_right
"1" Tri_down
"2" Tri_up
"3" Tri_left
"4" Tri_right
"8" Octagon
"S" Square
"P" Pentagon
“*” Star
"H" Hexagon1
H Hexagon2
+ Plus
"X" X
D Diamond
"D" Thin_diamond
| VLine
“_” Hline
Tickleft Tickleft
Tickright Tickright
Tickup Tickup
Tickdown Tickdown
Caretleft Caretleft
Caretright Caretright
Caretup Caretup
Caretdown Caretdown
"None" Nothing
None Nothing
” “ Nothing
“” Nothing
' $...$ ' Render the string using Mathtext.
Verts A list of (x, y) pairs used for Path vertices. The center of the marker is located at (0,0) and the size is normalized.
Path A Path instance.
(numsides, style, angle) See below

Http://matplotlib.org/api/markers_api.html#module-matplotlib.markers

3. Populating the data

Can be combined with numpy to quickly fill the data, draw a graph

>>> Import NumPy as NP
>>> Import Matplotlib.pyplot as Plt
>>> x=np.arange (0, 4, 0.05)>>>Xarray ([0. , 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4 , 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1. , 1.05, 1.1, 1.15, 1.2, 1.25, 1.3 , 1.35, 1.4, 1.45, 1.5, 1.55, 1.6, 1.65, 1.7, 1.75, 1.8, 1.85, 1.9, 1.95, 2. , 2.05, 2.1, 2.15, 2.2 , 2.25, 2.3, 2.35, 2.4, 2.45, 2.5, 2.55, 2.6, 2.65, 2.7, 2.75, 2.8, 2.85, 2.9, 2.95, 3. , 3.05, 3.1 , 3.15, 3.2, 3.25, 3.3, 3.35, 3.4, 3.45, 3.5, 3.55, 3.6, 3.65, 3.7, 3.75, 3.8, 3.85, 3.9, 3.95])>>> plt.plot (x, Np.sin (0.5 * Np.pi *x)) [<matplotlib.lines.line2d Object at 0x11314a810>]>>> Plt.show ()

4. Line Style

Controlling Line Properties

Lines has many attributes that can set:linewidth, dash style, antialiased, etc; See Matplotlib.lines.Line2D. There is several ways to set line properties

  • use keyword args:

     plt. (xy Linewidth=2.0)       
  • use the setter methods of THE&NBSP;line2d  instance. plot  returns a list of Lines E.G.,&NBSP;line1, line2  = plot (x1,y1,x2,y2) . Below I has only one line so it is a list of length 1. I use a tuple unpacking in the line, =  plot (X, y,  ' O ')  to Get the first element of the list:

    Lineplt.  Plot(xy'-')line.  set_antialiased(False# Turn off antialising         
  • Use the setp () command. The example below uses a Matlab-style command to set multiple properties on a list of lines. SETPworks transparently with a list of objects or a single object. You can either use Python keyword arguments or matlab-style string/value pairs:

    Lines=Plt.Plot(X1,y1x2y2 # use keyword Argsplt. Setp (linescolor=  ' R ' linewidth=2.0 ) # or MATLAB style string value Pairsplt. Setp (lines ' color '  ' R '  ' linewidth ' 2.0 )               /span>                

Here is the available line2d properties.

Property
Value Type
Alpha Float
Animated [True | False]
antialiased or AA [True | False]
Clip_box A Matplotlib.transform.Bbox instance
clip_on [True | False]
Clip_path A Path instance and a Transform instance, a Patch
Color or C Any matplotlib color
Contains The hit testing function
Dash_capstyle [' Butt ' | ' Round ' | ' projecting ']
Dash_joinstyle [' miter ' | ' Round ' | ' bevel ']
Dashes Sequence of On/off ink in points
Data (Np.array xdata, Np.array ydata)
Figure A Matplotlib.figure.Figure instance
Label Any string
LineStyle or LS [ '-' | '---' | '-. ' | ': ' | ' Steps ' | ...]
LineWidth or LW Float value in points
Lod [True | False]
Marker [ ' + ' | ', ' | '. ' | ' 1 ' | ' 2 ' | ' 3 ' | ' 4 ' ]
Markeredgecolor or MEC Any matplotlib color
Markeredgewidth or Mew Float value in points
Markerfacecolor or MFC Any matplotlib color
Markersize or MS Float
Markevery [None | integer | (Startind, Stride)]
Picker Used in interactive line selection
Pickradius The line pick selection radius
Solid_capstyle [' Butt ' | ' Round ' | ' projecting ']
Solid_joinstyle [' miter ' | ' Round ' | ' bevel ']
Transform A Matplotlib.transforms.Transform instance
Visible [True | False]
XData Np.array
Ydata Np.array
ZOrder Any number

Drawing: Matplotlib

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.