Scientific computing library learning summary

.Numpy library and a library of learning matplotlib

  (1) numpy Library Introduction: scientific computing package that supports N-dimensional array operation, processing large matrix, mature broadcast libraries, vector operations, linear algebra, Fourier transform, random number generation, and with no C ++ / Fortran language seam binding

      np.array ([1,2,3]) list into an array; np.array ((1,2,3)) tuple into an array; np.array (range (5)) to convert an array of object range ; np.arange (8) similar to the built-in range () function

      np.linspace (0,10,11, endpoint = False) does not include end point arithmetic array

 

  (2) matplotlib Library Introduction: Python programming language is extended and numerical mathematics package  NumPy visual interface. It is a graphical user interface using a common tool packages, such as Tkinter, wxPython, Qt GTK +, or the application provides embedded graphics application program interface (API). In addition, it matplotlib also based image processing library (such as Open Graphics Library OpenGL) of pylab interface that is very similar design with MATLAB - though not how to use. SciPy is graphically drawn with matplotlib.

    

 

Binding two. Examples of the use of two libraries

# - * - Coding: UTF-. 8 - * - 
"" " 
Spyder Editor 

. This Temporary IS A Script File 
" "" 
Print ( " Start " ) 

Import numpy AS NP
 Import matplotlib.pyplot AS PLT
 Import matplotlib 

matplotlib.rcParams [ ' font.family ' ] = ' SimHei '  # set the default font 
matplotlib.rcParams [ ' font.sans serif- ' ] = [ ' SimHei ' ] # set the default font 
Labels np.array = ([ 'First week ', ' The second week ' , ' third week ' , ' the fourth week ' , ' the fifth week ' , ' six weeks ' , ' seventh week ' ]) #
 . 7 nAttr = # Number edge 
data = np.array ([95,85,90,95,80,85,100]) # data value 
angles np.linspace = (0,2 * np.pi, nAttr, Endpoint = False) # angle setting, 0-2PI, the partition 7 
data np.concatenate = ((data, [data [0]]))   # the angle of the array data and a closed end to end, to facilitate drawing the plot function with 
angles = np.concatenate ((angles, [angles [0]])) 
Fig = plt.figure (facecolor =" Pink " )    # periphery of the outer color graphics 
plt.subplot (111, Polar = True)     # create sub-partitions polar coordinate system 
plt.plot (Angles, Data, ' BO- ' , Color = ' G ' , as linewidth = 2 ) # according to the angle data and irregular polygons drawn 
plt.fill (angles, data, facecolor = ' G ' , Alpha = 0.25)   # fill color 
plt.thetagrids (180 [angles * / np.pi, labels)   # settings tab e.g. week X 
plt.figtext (0.52,0.95, ' 14-the Kind FIG results ' , HA = ' Center ' ) # set title 
 plt.grid (True)
plt.savefig ('dota_radar.JPG')
plt.show()

Results pictures:

 

III. Custom hand-painted wind

code show as below:

# - * - Coding: UTF-. 8 - * - 
Import numpy AS NP
 from the PIL Import Image 

class Picture: 
    
    DEF  the __init__ (Self, position): 
        self.position = position 

    DEF Hand_drawn_style (Self): 
        vec_el = np.pi / 2    # a top angle of the light source, the radian value 
        vec_az = np.pi /. 3     # source azimuth angle, in radians 
        depth. 6 =           # depth weight, the smaller the value the closer to white background area, the greater the value of the background region closer to black 
        im = Image .Open (self.position) .convert ( ' L ' )      # is opened and converted to grayscale images
        np.asarray = A (IM) .astype ( ' a float ' ) 
        Grad = np.gradient (A)               # fetches the image gray value gradient 
        grad_x, grad_y = Grad               # respectively horizontal and vertical gradient image taking a value 
        grad_x = grad_x * depth / 100 . 
        grad_y = grad_y depth * / 100 . 
        DX = np.cos (vec_el) * np.cos (vec_az) # source of x-axis impact 
        Dy = np.cos (vec_el) * np.sin (vec_az) # source effect on the y-axis 
        DZ = np.sin (vec_el)                   # source of the z-axis effect 
        a = np.sqrt (grad_x grad_y ** ** 2 + 2 +. 1 .) 
        uni_xGrad_x = / A 
        uni_y = grad_y / A 
        uni_z = 1. / A 
        A2 = 255 * (DX + Dy uni_x * * * uni_z uni_y + DZ) # source normalized 
        A2 a2.clip = (0, 255)                  # preventing overflow 
        = Image.fromarray IM2 (a2.astype ( ' uint8 ' ))        # reconstructed image 
        im2.save ( ' 4d0424dd81_.jpg ' ) # save image 
        im2.show ()                 # display image 

IF  the __name__ == ' __main__ ' :
    position = '4d0424dd81.jpg'
    picture(position).Hand_drawn_style()

Renderings:

 

 

IV. Distribution plot

Code

import numpy as np
import matplotlib
import scipy.stats
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

if __name__ == "__main__":
    # 期望0,标准差1,条数50
    mu, sigma, num_bins = 0, 1, 500
    # 1M个随机数
    x = mu + sigma * np.random.randn(10000)

    # 正态分布的数据, 颜色的透明度0.5
    n, bins, patches = plt.hist(x, num_bins, density=True, facecolor='blue', alpha=0.5)
 # Histogram function, x is the x-axis value, normed = 1 expressed as a probability density, 
# i.e., and as a green square, color depth parameter 0.5. N return probability, the value of x straight line of the left block, and each the object block 
    # fit curve 
    Y = scipy.stats.norm.pdf (bins, MU, Sigma) 
    plt.plot (bins, Y, ' r-- ' ) 
    plt.xlabel ( ' Expectation ' ) 
    plt.ylabel ( ' Probability ' ) 
    plt.title ( ' Histogram of Normal Distribution: $ \ MU = $ 0, $ \ Sigma. 1 = $ ' ) 

    plt.subplots_adjust (left = 0.15) # left margin 
    plt.grid (True)   # open grid lines 
    plt .show ()

Results pictures

Guess you like

Origin www.cnblogs.com/alinger/p/10932048.html