当前位置:网站首页>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 】
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 】
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 】
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 】
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 】
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 】
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 
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 】
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 】
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 】
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 】
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 】
3D chart
边栏推荐
猜你喜欢

行锁与隔离级别案例分析

Properties file garbled

wm_concat()和group_concat()函数

RuntimeError: CUDA error: out of memory自己的解决方法(情况比较特殊估计对大部分人不适用)

贝叶斯网络详解

DVD digital universal disc

图像二值化处理

Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance!

CD-CompactDisk

Applet setting button sharing function
随机推荐
Row lock analysis and deadlock
JS cast
利用递归实现求n位所有格雷码
DVD digital universal disc
tag动态规划-刷题预备知识-2. 0-1背包理论基础和二维数组解法模板
JVM入個門(1)
Padding percentage operation
Ethereum技术架构介绍
The cross compilation environment appears So link file not found problem
Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance!
Deep understanding of MySQL lock and transaction isolation level
Usage and difference between ros:: spinonce() and ros:: spin()
Properties file garbled
Eigen库计算两个向量夹角
JS common regular expressions
数字签名标准(DSS)
in和exsits、count(*)查询优化
MySQL的MVCC机制详解
(必须掌握的多线程知识点)认识线程,创建线程,使用Thread的常见方法及属性,以及线程的状态和状态转移的意义
Comparing the size relationship between two objects turns out to be so fancy