A brief introduction to the two external libraries NumPy and Matplotlib

Hits: 0

Use simple code to introduce some simple functions of the two

import numpy as np
 import matplotlib.pyplot as plt
 from matplotlib.image import imread
 #Numpy array (np.array) can generate an N-dimensional array, that is, an array of any dimension can be generated. Mathematically, one-dimensional arrays are generally referred to as vectors, and two-dimensional arrays are referred to as matrices 
. In addition, the generalized vectors or matrices can be collectively referred to as tensors. 
def  createNumpy () : 
    """
    generate numpy array
    :return:
    """
    x = np.array([1.0,2.0,3.0])
    print(x)
    print(type(x))

def createNumpyAddAnddecay():
    """
    numpy arithmetic operations
    :return:
    """
    x = np.array([1.0,2.0,3.0])
    y = np.array([ 2.0 , 4.0 , 6.0 ])
     #It should be noted that when the number of elements of x and y is the same, arithmetic operations can be performed on each element. If the number of elements is not the same, the program will report an error .
    print(x+y)
    print(x-y)
    print(x*y)
    print(x/y)
    #numpy can not only perform element-wise (corresponding element) operations, but also perform operations in combination with a single value. 
    # That is, the operation between each element of numpy and the scalar, this function is called broadcast 
    print(x/ 2.0 )
 def  createNumpyNdices () : 
    """
    generate multidimensional array
    :return:
    """
    A = np.array([[1,2],[3,4]])
    print(A)
    #Display the shape of matrix A through shape. If you have learned TensorFolw, you will feel familiar. (2,2) means to generate a 2*2 matrix.
    print(A.shape)
    #Also use dtype to view the data type of matrix elements
    print(A.dtype)
    #Arithmetic operation of multidimensional matrix 
    B = np.array([[ 3 , 0 ],[ 0 , 6 ]])
     #Corresponding addition
    print(A+B)
    #Corresponding multiplication, remember that here is not the overall multiplication of the two matrices, but the multiplication of the corresponding elements. That is A11*B11.
    print(A*B)
    #Matrix broadcast 
    print(A* 10 )

def createBroadcast():
    """
    Can operations be performed between arrays of different shapes? We mentioned earlier that an error will be reported if the number of elements is not the same.
    But for one-dimensional arrays, it can be extended to any size for calculation.
    A 2*2 matrix is ​​multiplied by 10 and the scalar 10 will be expanded into a 2*2 shape. This function is called broadcasting
    Because NumPy has the function of broadcasting, operations can also be performed smoothly between arrays of different shapes.
    :return:
    """
    A = np.array([[1,2],[3,4]])
    B = np.array([10,20])
    print(A*B)
def createVisttElement():
    """
    Elements are indexed from 0, and each element can be accessed as follows
    :return:
    """
    X = np.array([[51,55],[14,19],[0,4]])
    print(X)
    #Line 0 
    print(X[ 0 ])
     #The 0th element of line 0 
    print(X[ 0 ][ 0 ])
     #Use the for loop statement to access each element, and each line outputs the corresponding line. 
    for row in X:
        print(row)
    #NumPy can also use arrays to access individual elements 
    X = X.flatten() #Convert X to a one-dimensional array
    print(X)
    #Get elements with indices 0, 2, 4 
    print(X[np.array([ 0 , 2 , 4 ])])
     #Use conditional output 
    print(X[X> 15 ])

def matplotlibFirst():
    """
    simple drawing
    :return:
    """ #Generate 
    data 
    x = np.arange( 0 , 6 , 0.1 ) #Generate data from 0 to 6 in steps of 0.1
    y = np.sin(x)

    #Draw graphics, apply NumPy's sin function np.sin(), pass x, y data to plt.plot, and then draw graphics. 
    #Finally display through ply.show
    plt.plot(x,y)
    plt.show()
def matplotlibSinAndCos():
    """
    Add cos on the basis of sin, and add labels
    :return:
    """ #Prepare 
    data 
    x = np.arange( 0 , 6 , 0.1 )
    y1 = np.sin(x)
    y2 = np.cos(x)
    #Draw graphics 
    plt.plot(x,y1,label= "sin" )
    plt.plot(x,y2,linestyle = "--" ,label = "cos" ) #Draw cos with dotted lines 
    plt.xlabel( "x" ) #x axis label 
    plt.ylabel( "y" ) #y axis label 
    plt.title( 'sin&cos' ) #title
    plt.legend()
    plt.show()

def matplotilibImgShow():
    """
    read image
    :return:
    """
    img = imread('lena.png')
    plt.imshow(img)

    plt.show()
def main():
    matplotilibImgShow()


if __name__ == "__main__":
    main()

You may also like...

Leave a Reply

Your email address will not be published.