Introduction to matplotlib
matplotlib is python's most famous plotting library, it provides a set of command API similar to matlab, very suitable for interactive line drawing. It can also be easily embedded as a drawing control in GUI applications.
It's pretty well documented, and the Gallery page has hundreds of thumbnails, all of which have source programs when you open them. So if you need to draw a certain type of diagram, just browse/copy/paste it in this page and you're basically set.
The more famous data plotting tool under Linux is also gnuplot, which is free, and Python has a package that can call gnuplot, but the syntax is rather uncomfortable, and the quality of the drawing is not high.
Matplotlib, on the other hand, is stronger: the syntax of Matlab, the python language, the drawing quality of latex (you can also use mathematical formulas drawn by the embedded latex engine).
Installation method for the plotting library Matplotlib:strike (on the keyboard)here are
matplotlib plotting line graphs
1. line chart
import numpy as np import as plt x = (0, 2 * , 100) y1, y2 = (x), (x) (x, y1) (x, y2) ('line chart') ('x') ('y') ()
2. Legend
The legend can be drawn by specifying the label at plot time and then calling the legend method. Example:
import numpy as np import as plt x = (0, 2 * , 100) y1, y2 = (x), (x) (x, y1, label='y = sin(x)') (x, y2, label='y = cos(x)') () ()
The legend method accepts a loc keyword parameter to set the location of the legend, which can take the value of 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'
10: ‘center'
3. Line styles
(1) Color
The keyword parameter color (or c) of the plot method is used to set the color of the line. It can take the value:
1. Color name or abbreviation
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 to be between [0, 1].
4, [0, 1] between the floating point number of the string form, indicating the gray value. 0 means black, 1 means white
(2) Style
The keyword parameter linestyle (or ls) of the plot method is used to set the style of the line. It can take the value:
- -, solid
- --, dashed
- -., dashdot
- :, dotted
- '', ' ', None
(3) Coarse and fine
Setting the keyword parameter linewidth (or lw) of the plot method changes the thickness of the line, whose value is a floating-point number.
import numpy as np import as plt x = (0, 2 * , 100) y1, y2 = (x), (x) (x, y1, c='r', ls='--', lw=3) (x, y2, c='#526922', ls='-.') ()
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 marker can take the value:
- '.': 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
Example:
import numpy as np import as plt x = (0, 2 * , 10) y1, y2 = (x), (x) (x, y1, marker='o', mec='r', mfc='w') (x, y2, marker='*', ms=10) ()
In addition, the marker keyword argument can be combined with the color and linestyle keyword arguments into a single string. For example:
import numpy as np import as plt x = (0, 2 * , 10) y1, y2 = (x), (x) (x, y1, 'ro-') (x, y2, 'g*:', ms=10) ()
summarize
The above is the entire content of this article, I hope that the content of this article on your learning or work can bring some help, if there are questions you can leave a message to exchange, thank you for my support.