Study Notes for Numpy and Matplotlib Libraries

What is NumPy?

NumPy is a powerful Python library mainly used to perform calculations on multidimensional arrays. The word NumPy comes from two words --  Numericaland Python. NumPy provides a large number of library functions and operations that help programmers easily perform numerical computations. Such numerical computations are widely used for the following tasks:

  • Machine Learning Models: When writing machine learning algorithms, various numerical computations on matrices are required. For example, matrix multiplication, transposition, addition, etc. NumPy provides a very good library for simple (in terms of writing code) and fast (in terms of speed) computations. NumPy arrays are used to store training data and parameters of machine learning models.

  • Image Processing and Computer Graphics: Images in computers are represented as multidimensional arrays of numbers. NumPy becomes the most natural choice in the same situation. In fact, NumPy provides some excellent library functions for fast processing of images. For example, mirror images, rotate images by a specific angle, etc.

  • Mathematical tasks: NumPy is useful for performing various mathematical tasks such as numerical integration, differentiation, interpolation, extrapolation, etc. So it forms a quick alternative to Python-based MATLAB when it comes to math tasks.

For more details, please refer to the official Chinese document  https://www.numpy.org.cn/index.html , or the official English document  https://www.numpy.org/

what is matplotlib?

Matplotlib is a Python 2D plotting library that can generate publication-quality data in a variety of hardcopy formats and cross-platform interactive environments. Matplotlib is available for Python scripts, Python and IPython shells, Jupyter notebooks, web application servers, and four GUI toolkits.

Matplotlib tries to make simple and easy things possible. You can generate plots, histograms, power spectra, bar graphs, error plots, scatter plots and more with just a few lines of code. See the Sample Gallery and Thumbnail Gallery for examples .

For simple plotting, the pyplot module provides a MATLAB-like interface, especially when combined with IPython. For advanced users, you have full control over line styles, font properties, axis properties, and more through an object-oriented interface or through a set of functions familiar to MATLAB users.

For more details, please refer to the official Chinese document  https://www.matplotlib.org.cn/index.html , or the official English document  https://matplotlib.org/

Example application

# -*- coding: utf-8 -*-
"""
Created on Thu Apr 18 17:28:39 2019

@author: haiwe
"""

#DrawRadar
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

matplotlib.rcParams['font.family'] = 'SimHei'
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
labels = np.array([ ' Week 1 ' , ' Week 2 ' , ' Week 3 ' , ' Week 4 ' , ' Week 5 ' , ' Week 6 ' ])
nAttr = 6 
data = np.array([50, 90.9, 100, 97, 64.4, 65])   #Data array 
angles = np.linspace(0, 2*np.pi, nAttr, endpoint = False)
data = np.concatenate((data, [data[0]]))
angles = np.concatenate((angles, [angles[0]]))
fig = plt.figure(facecolor = 'white')
plt.subplot(111, polar = True)
plt.plot(angles, data, 'bo-',color = 'g', linewidth = 2)
plt.fill(angles, data, facecolor = 'g', alpha = 0.25)
plt.thetagrids(angles * 180/np.pi, labels)
plt.figtext( 0.52, 0.95, ' 32 Kangcheng Jia Shi results radar chart ' , ha = ' center ' )
plt.grid(True)
plt.savefig( ' mark_radar.jpg ' ) #Save the picture
plt.show()

In fact, the Numpy library and the Matplotlib library are powerful, and I will add them when I have time.