当前位置:网站首页>Numpy's Matplotlib

Numpy's Matplotlib

2022-06-26 18:17:00 Little fox dreams of going to fairy tale town

Matplotlib yes Python Drawing library of , It is associated with Numpy Use it together
【 Cited example 】
Draw y=2*x+5 Function image of

import numpy as np
from matplotlib import pyplot as plt

# Set abscissa value 、 Determine the function expression 
x = np.arange(1, 11)
y = 2 * x + 5
# Add the title 、 Abscissa 、 Ordinate label 
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
# Draw a function image 
plt.plot(x, y)
plt.show()

【 Running results 】
 Insert picture description here

 If you want the function image to appear as dots , Instead of lines , You need to 

plt.plot(x,y) Change it to plt.plot(x,y,'ob')

【 Running results 】
 Insert picture description here

Sine wave y=sin(x)

import numpy as np
from matplotlib import pyplot as plt

# Set abscissa value 、 Determine the function expression 
x = np.arange(0, 3*np.pi,0.1)
y = np.sin(x)
# Add the title 、 Abscissa 、 Ordinate label 
plt.title("y=sin(x)")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
# Draw a function image 
plt.plot(x, y)
plt.show()

【 Running results 】
 Insert picture description here

 The cosine function simply takes 
y = np.sin(x)  Change it to  y = np.cos(x) that will do 

subplot() function

application subplot() function , Draw different things in the same picture

# ReLU and Sigmoid Activation function diagram 
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches

# Set picture size 
plt.figure(figsize=(6, 4))

# x yes 1 Dimension group , The array size is from -10.  To 10. The real number , every other 0.1 Take a point 
x = np.arange(-10, 10, 0.1)
#  Calculation  Sigmoid function 
s = (np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x))


#  Calculation ReLU function 
y = np.clip(x, a_min = 0., a_max = None)

#########################################################
#  The following part is the drawing program 

#  Set up two subgraph windows , take Sigmoid The function image of is drawn on the top 
f = plt.subplot(2,1,1)
#  Draw function curve 
plt.plot(x, s, color='r')
#  Add a text description 
plt.text(-5., 0.9, r'$y=sigmoid(x)$', fontsize=13)
#  Format axis 
currentAxis=plt.gca()
currentAxis.xaxis.set_label_text('x', fontsize=15)
currentAxis.yaxis.set_label_text('y', fontsize=15)

#  take ReLU The function image of is drawn below 
f = plt.subplot(2,1,2)
#  Draw function curve 
plt.plot(x, y, color='g')
#  Add a text description 
plt.text(-3.0, 9, r'$y=ReLU(x)$', fontsize=13)
#  Format axis 
currentAxis=plt.gca()
currentAxis.xaxis.set_label_text('x', fontsize=15)
currentAxis.yaxis.set_label_text('y', fontsize=15)

plt.show()

【 Running results 】
 Insert picture description here

Common picture

from matplotlib import pyplot as plt
import numpy as np
n = 256  #n It can be understood as the smoothness of the curve 
X = np.linspace(-np.pi,np.pi,n,endpoint=True)
Y = np.sin(2*X)

plt.plot(X,Y+1,color='red',alpha=1.00)
plt.plot(X,Y-1,color='blue',alpha=1.00)
plt.show()

【 Running results 】
 Insert picture description here

Bar chart

from matplotlib import pyplot as plt
# Data sets 1
x =  [5,8,11]  # Abscissa 
y =  [12,16,6] # Corresponding value 
# Data sets 2
x2 =  [6,9,12] # Abscissa 
y2 =  [6,15,7] # Corresponding value 
plt.bar(x, y, color = 'r', align =  'center')
plt.bar(x2, y2, color =  'g', align =  'center')
plt.title('Bar graph')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()

【 Running results 】
 Insert picture description here

Scatter plot

from matplotlib import pyplot as plt
import numpy as np

n = 1024
X = np.random.normal(0,30,n)
Y = np.random.normal(0,30,n)

plt.title('scale:30')
plt.scatter(X,Y,color='red')
plt.show()

【 Running results 】 The following are the variances 5,10,15,20,25,30 The scatter diagram of
 Insert picture description here

Contour map

from matplotlib import pyplot as plt
import numpy as np

def f(x,y):return(1-x/2+x**5+y**3)*np.exp(-x**2-y**2) # Contour model 

n = 256  #n It can be understood as the smoothness of the curve 
x = np.linspace(-3,3,n)
y = np.linspace(-3,3,n)
X,Y = np.meshgrid(x,y)

plt.contourf(X,Y,f(X,Y),8,alpha=.75,cmap='jet')
C = plt.contour(X,Y,f(X,Y),8,color='black',linewidth=.5)
plt.show()

【 Running results 】
 Insert picture description here

grayscale

from matplotlib import pyplot as plt
import numpy as np

def f(x,y):return(1-x/2+x**5+y**3)*np.exp(-x**2-y**2)

n = 10
x = np.linspace(-3,3,4*n)
y = np.linspace(-3,3,4*n)
X,Y = np.meshgrid(x,y)

plt.imshow(f(X,Y))
plt.show()

【 Running results 】
 Insert picture description here

The pie chart

from matplotlib import pyplot as plt
import numpy as np

n = 20  #n Determines how many partitions there are , The code shows 20 Zones 
Z = np.random.uniform(0,1,n)
plt.pie(Z)
plt.show()

【 Running results 】
 Insert picture description here

Measurement field diagram

from matplotlib import pyplot as plt
import numpy as np

n = 8
X,Y = np.mgrid[0:n,0:n]
plt.quiver(X,Y,color="red")
plt.show()

【 Running results 】
 Insert picture description here

Polar chart

from matplotlib import pyplot as plt
import numpy as np

plt.figure(figsize=(4,4))
ax1 = plt.subplot(111,projection='polar')
ax1.set_title('spot fish')
ax1.set_rlim(0,12)

data = np.random.randint(1,10,10)
theta = np.arange(0,2*np.pi,2*np.pi/10)

bar = ax1.bar(theta,data,alpha=0.5)
for r,bar in zip(data,bar):
    bar.set_facecolor(plt.cm.jet(r/10.))
plt.show()

【 Running results 】
 Insert picture description here

3D chart

原网站

版权声明
本文为[Little fox dreams of going to fairy tale town]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261807468408.html