Introduction
Matplotlib is a very useful Python drawing library. It combines well with numpy, but is itself a separate open source project. You can visit the Gallery of Matplotlib official website to view wonderful sample pictures and code
There are some function functions in matplotlib to download and process data from Yahoo Finance channel (back to research)
Simple Drawing
The Matplotlib.pyplot package contains a simple drawing function. It is to be remembered that the function that is subsequently called will change the current drawing. Eventually, we'll save the drawing to a file or show it using the show function. However, if we are using Ipython running on the QT or WX backend, the graph will be updated interactively without waiting for the result of the show function. This is similar to the way the output text on the screen can be continuously printed out.
1 Drawing an image of a polynomial function
Create a polynomial function using NumPy's ploy1d
Import NumPy as NP
import Matplotlib.pyplot as plt
func = np.poly1d (Np.array ([1,2,3,4]). Astype (float)) # The coefficients of the natural number sequence as the polynomial
x = Np.linspace ( -10,10,30) #使用linspace函数创建X轴的数值, 30 evenly distributed values between 10 and 10
y = func (x) # Computes the value of the polynomial that we created in the first step
plt.plot (x, y) #调用plot函数, which does not immediately show the function image
Plt.xlabel (' x ') #使用xlable, ylable function add tag
Plt.ylabel (' Y (x) ')
plt.show () #调用show函数显示函数图像
2 plotting polynomial functions and their derivative functions
The Tip:plot function can accept any number of arguments. In the previous section, we gave two parameters. We can also use the optional format string parameter to specify the color and style of the line, by default B-the blue solid line. You can specify for other colors and styles, such as red dashed lines.
Using the derive function and the parameter m=1 to obtain the first order function, and the two styles of different curves to distinguish two function curve
Import NumPy as NP
import Matplotlib.pyplot as plt
func = np.poly1d (Np.array ([1,2,3,4]). Astype (float))
func1 = Func.deriv (m=1)
x = Np.linspace ( -10,10,30)
y = func (x)
Y1 = func1 (×)
Plt.plot (x, y, ' ro ', x,y1, ' g--') #红色圆形和绿色虚线
plt.xlabel (' x ')
plt.ylabel (' y ')
plt.show ()
Sub-chart
You may encounter too many curves in the diagram, where we can use the subplot function to draw them in groups.
Drawing polynomial functions and their multiple derivative images
Draw a polynomial function and its first and second derivative functions, and draw 3 sub-graphs
#创建多项式函数及其导函数们
import NumPy as NP
import Matplotlib.pyplot as plt
func = np.poly1d (Np.array ([1,2,3,4]). Astype (float))
func1 = Func.deriv (m=1)
Func2 = Func.deriv (m=2)
x = Np.linspace ( -10,10,30)
y = func (x)
Y1 = func1 (x)
y2 = func2 (x)
Use the subplot function to create the first sub-graph. The first parameter of the function is the number of rows of the child graph, the second argument is the number of columns of the sub-graph, and the third argument is a 1-based ordinal. Another way is to combine the 3 parameters into a single number, such as 311. The sub-diagram will then be organized into 3 rows and 1 columns. Set the caption of the sub-graph to polynomial, drawn with a solid red line.
Plt.subplot (311)
plt.plot (x, Y, ' R ') #红色实线
plt.title (' polynomial ')
Use the subplot function to create a second sub-graph. Set the title of the sub-graph to first derivative, drawn with a blue triangle
Plt.subplot (312)
Plt.plot (x,y1, ' b^ ') #蓝色三角形
plt.title (' first derivative ')
Use the subplot function to create a third sub-graph. Set the caption of the sub-graph to second derivative, drawn with a green circle
Plt.subplot (313)
Plt.plot (x,y2, ' go ') #绿色圆形
plt.title (' Second derivative ')
Plt.xlabel (' x ')
Plt.ylabel (' y ')
plt.show ()