Python uses matplotlib to draw a line chart tutorial, pythonmatplotlib

Source: Internet
Author: User
Tags install matplotlib

Python uses matplotlib to draw a line chart tutorial, pythonmatplotlib

Introduction to matplotlib

Matplotlib is the most famous plotting library in python. It provides a complete set of command APIs similar to matlab and is very suitable for interactive plotting. It can also be easily used as a drawing control and embedded into GUI applications.

Its documentation is quite complete, and there are hundreds of thumbnails on the Gallery page. After opening it, all the source programs are available. Therefore, if you want to draw a certain type of graph, you can simply browse, copy, and paste the graph on this page.

In Linux, the famous Data graph tool also has gnuplot, which is free of charge. Python has a package that can call gnuplot, but the syntax is not used to it and the drawing quality is not high.

Matplotlib is relatively strong: Matlab syntax, python language, latex drawing quality (you can also use the built-in latex engine to draw mathematical formulas ).

How to install Matplotlib In the Drawing Library:Click here

Matplotlib

1. line chart

import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0, 2 * np.pi, 100)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 the label during the plot, 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, 100)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 an loc keyword parameter to set the legend position. values can be numbers or strings:

0: 'best'

1: 'Upper right'

2: 'Upper left'

3: 'lower left'

4: 'lower'

5: 'right'

6: 'center left'

7: 'center right'

8: 'lower Center'

9: 'Upper Center'

10: 'center'

3. Line Style

(1) color

The keyword parameter color (or c) of the plot method is used to set the line color. Optional values:

1. color name or short name

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 between [0, 1]

4. The string format of the floating point between [0, 1], indicating the gray value. 0 indicates black, 1 indicates white

(2) style

The keyword parameter linestyle (or ls) of the plot method is used to set the line style. Optional values:

  • -, Solid
  • --, Dashed
  • -., Dashdot
  • :, Dotted
  • '','', None

(3) Width

You can set the keyword parameter linewidth (or lw) of the plot method to change the line width. Its value is a floating point number.

import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0, 2 * np.pi, 100)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 marker style:

  • Marker
  • Markeredgecolor or mec
  • Markeredgewidth or mew
  • Markerfacecolor or mfc
  • Markerfacecoloralt or mfcalt
  • Markersize or MS

The value of marker 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, 10)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 color and linestyle keywords into a string. For example:

import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0, 2 * np.pi, 10)y1, y2 = np.sin(x), np.cos(x)plt.plot(x, y1, 'ro-')plt.plot(x, y2, 'g*:', ms=10)plt.show()

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.