Matplotlib is a Python toolbox for data visualization in scientific computing. With it, Python can plot a variety of data graphics, such as Matlab and Ave. The following article describes how to use matplotlib to draw a line chart in python. For more information, see. Matplotlib is a Python toolbox for data visualization in scientific computing. With it, Python can plot a variety of data graphics, such as Matlab and Ave. The following article describes how to use matplotlib to draw a line chart in python. For more information, see.
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()
(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:
The value of marker is:
'.': Point marker
',': Pixel marker
'O': circle marker
'V': triangle_down marker
Pai_^ _ ^ ': 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()
For more articles about how to use matplotlib to draw a line chart in python, refer to PHP!