Python using matplotlib to draw line chart tutorials

Source: Internet
Author: User
Tags cos sin
Matplotlib is a Python toolbox for data visualization of scientific calculations. With it, Python can draw a wide variety of data graphics, such as MATLAB and Octave. The following article mainly describes how Python uses matplotlib to draw a line chart, a tutorial that needs friends to reference.

Matplotlib Introduction

Matplotlib is Python's most famous drawing library, which provides a complete set of command APIs similar to those of MATLAB, and is ideal for interactive line mapping. It can also be easily used as a drawing control, embedded in GUI applications.

Its documentation is quite complete, and there are hundreds of thumbnails on the gallery page, and the source program opens. So if you need to draw some kind of diagram, just browse/copy/paste it on this page and basically get it done.

Under Linux more famous data graph tool also has gnuplot, this is free, Python has a package can call gnuplot, but the syntax is not used, and the drawing quality is not high.

And matplotlib is stronger: Matlab syntax, Python language, latex paint quality (you can also use the built-in latex engine to draw the mathematical formula).

How to install the drawing library Matplotlib: click here

Matplotlib Draw a line chart

1. Line chart

Import NumPy as Npimport matplotlib.pyplot as Pltx = Np.linspace (0, 2 * np.pi, +) y1, y2 = np.sin (x), Np.cos (x) plt.plot (x , y1) plt.plot (x, y2) plt.title (' Line chart ') Plt.xlabel (' x ') Plt.ylabel (' Y ') plt.show ()

2. Legend

Specify a label at plot time, and then call the legend method to draw the legend. For example:

Import NumPy as Npimport matplotlib.pyplot as Pltx = Np.linspace (0, 2 * np.pi, +) y1, y2 = np.sin (x), Np.cos (x) plt.plot (x , Y1, label= ' y = sin (x) ') Plt.plot (x, y2, label= ' y = cos (x) ') Plt.legend () plt.show ()


The Legend method accepts a loc keyword parameter to set the location of the legend, preferably a number or a string:

0: ' Best '

1: ' Upper right '

2: ' Upper left '

3: ' Lower left '

4: ' Lower right '

5: ' Right '

6: ' Center left '

7: ' Center right '

8: ' Lower center '

9: ' Upper center '

Ten: ' Center '

3. Style of line

(1) Color

The keyword parameter of the plot method color (or c) is used to set the colors of the lines. The desirable values are:

1. Color name or shorthand

B:blue

G:green

R:red

C:cyan

M:magenta

Y:yellow

K:black

W:white

2. #rrggbb

3, (R, G, b) or (R, G, B, a), where R G B A is taken between [0, 1]

4, [0, 1] The string form of the floating-point number, representing the grayscale value. 0 = black, 1 for White

(2) style

The keyword parameter of the plot method LineStyle (or LS) is used to set the style of the line. The desirable values are:

    • -, solid

    • --, Dashed

    • -., Dashdot

    • :, Dotted

    • ",", None

(3) Thickness

Setting the keyword parameter of the plot method LineWidth (or LW) can change the thickness of the line and its value is a floating-point number.

Import NumPy as Npimport matplotlib.pyplot as Pltx = Np.linspace (0, 2 * np.pi, +) y1, y2 = np.sin (x), Np.cos (x) plt.plot (x , Y1, c= ' R ', ls= '--', lw=3) plt.plot (x, y2, c= ' #526922 ', ls= '-. ') Plt.show ()


4. Marker

The following keyword parameters can be used to set the style of the marker:

    • Marker

    • Markeredgecolor or MEC

    • Markeredgewidth or Mew

    • Markerfacecolor or MFC

    • Markerfacecoloralt or Mfcalt

    • Markersize or MS

Where the marker value is:

    • '. ': Point marker

    • ', ': pixel marker

    • ' O ': Circle marker

    • ' V ': Triangle_down marker

    • ' ^ ': triangle_up marker

    • ' < ': Triangle_left marker

    • ' > ': triangle_right marker

    • ' 1 ': Tri_down marker

    • ' 2 ': tri_up marker

    • ' 3 ': Tri_left marker

    • ' 4 ': tri_right marker

    • ' s ': square marker

    • ' P ': Pentagon marker

    • ' * ': Star marker

    • ' H ': hexagon1 marker

    • ' H ': hexagon2 marker

    • ' + ': plus marker

    • ' X ': x marker

    • ' D ': Diamond marker

    • ' d ': Thin_diamond marker

    • ' | ': VLine marker

    • ' _ ': hline marker

For example:

Import NumPy as Npimport matplotlib.pyplot as Pltx = Np.linspace (0, 2 * np.pi, ten) y1, y2 = np.sin (x), Np.cos (x) plt.plot (x, Y1, marker= ' o ', mec= ' R ', mfc= ' W ') plt.plot (x, y2, marker= ' * ', ms=10) plt.show ()


In addition, the Marker keyword parameter can be combined with the two keyword parameters, color and LineStyle, into a single string. For example:

Import NumPy as Npimport matplotlib.pyplot as Pltx = Np.linspace (0, 2 * np.pi, ten) y1, y2 = np.sin (x), Np.cos (x) plt.plot (x, Y1, ' ro-') plt.plot (x, y2, ' g*: ', ms=10) plt.show ()

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.