1. Simple drawing function
1 Import Matplotlib.pyplot as Plt 2 Import NumPy as NP 3 x = Np.linspace ( -1, 1,)4 y1 = x + 15plt.plot (x, y1)6 Plt.show ()
Np.linspace (-1, 1, 50)
Draw a function y=x+1 from 1 to 1 for 50 points
Import Matplotlib.pyplot as Plt Import = Np.linspace ( -1, 1, 5)
= x1 * x1
y2 = x2 * X2plt.plot (x1, y1)
Plt.plot (x2, y2) plt.show ()
Change a function, change to y=x*x, can more intuitively feel the function of the third parameter of Np.linspace ()
Np.linspace () The third parameter is 5: Blue
Np.linspace () The third parameter is 50: red
2. Modify the style of the line
Add a style directly to Plt.plot ()
1 ImportMatplotlib.pyplot as Plt2 ImportNumPy as NP3X1 = Np.linspace (-1, 1, 5)4x2 = Np.linspace (-1, 1, 50)5Y1 = x1 *X16y2 = x2 *X27 plt.plot (x1, y1)8Plt.plot (x2, y2, color="Gray", linewidth=10, linestyle="--")9Plt.show ()
Color: Colors
LineWidth: line width
LineStyle: Line Style
After the modified image is
3. Modifying axes
1 ImportMatplotlib.pyplot as Plt2 ImportNumPy as NP3X1 = Np.linspace (-1, 1, 50)4Y1 = x1 *X15 plt.plot (x1, y1)6Plt.xlim (-1, 10)7Plt.ylim (-2, 3)8 9Plt.xlabel ("I am x")TenPlt.ylabel ("I am y") OnePlt.show ()
Plt.xlim (): Range of x-axis
Plt.ylim (): Range of y-axis
Plt.xlabel:x Axis Callout
Plt.ylabel:y Axis Callout
Modify the value of an axis
1 ImportMatplotlib.pyplot as Plt2 ImportNumPy as NP3X1 = Np.linspace (-1, 1, 50)4Y1 = x1 *X15 plt.plot (x1, y1)6Plt.xlim (-1, 2)7Plt.ylim (-2, 3)8 9Plt.xlabel ("I am x")TenPlt.ylabel ("I am y") One ANew_ticks = Np.linspace (-1, 2, 5) - plt.xticks (new_ticks) -Plt.yticks ([-2, 0, 2, 3], ["very Bad","just so","Good","Perfect"]) thePlt.show ()
Change the font and add special symbols (note escaping) don't mention it.
Matplotlib Python Basic usage