当前位置:网站首页>Numpy之matplotlib
Numpy之matplotlib
2022-06-26 18:08:00 【小狐狸梦想去童话镇】
Matplotlib是Python的绘图库,它与Numpy一起使用
【引例】
画出y=2*x+5的函数图像
import numpy as np
from matplotlib import pyplot as plt
#设置横坐标取值、确定函数表达式
x = np.arange(1, 11)
y = 2 * x + 5
#添加标题、横坐标、纵坐标标签
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
#画函数图像
plt.plot(x, y)
plt.show()
【运行结果】
如果需要函数图像以圆点呈现,而不是用线呈现,则需要将
plt.plot(x,y)改为plt.plot(x,y,'ob')
【运行结果】
正弦波 y=sin(x)
import numpy as np
from matplotlib import pyplot as plt
#设置横坐标取值、确定函数表达式
x = np.arange(0, 3*np.pi,0.1)
y = np.sin(x)
#添加标题、横坐标、纵坐标标签
plt.title("y=sin(x)")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
#画函数图像
plt.plot(x, y)
plt.show()
【运行结果】
余弦函数只需将
y = np.sin(x) 改为 y = np.cos(x)即可
subplot()函数
应用subplot()函数,在同一图中绘制不同的东西
# ReLU和Sigmoid激活函数示意图
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
#设置图片大小
plt.figure(figsize=(6, 4))
# x是1维数组,数组大小是从-10. 到10.的实数,每隔0.1取一个点
x = np.arange(-10, 10, 0.1)
# 计算 Sigmoid函数
s = (np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x))
# 计算ReLU函数
y = np.clip(x, a_min = 0., a_max = None)
#########################################################
# 以下部分为画图程序
# 设置两个子图窗口,将Sigmoid的函数图像画在上边
f = plt.subplot(2,1,1)
# 画出函数曲线
plt.plot(x, s, color='r')
# 添加文字说明
plt.text(-5., 0.9, r'$y=sigmoid(x)$', fontsize=13)
# 设置坐标轴格式
currentAxis=plt.gca()
currentAxis.xaxis.set_label_text('x', fontsize=15)
currentAxis.yaxis.set_label_text('y', fontsize=15)
# 将ReLU的函数图像画在下边
f = plt.subplot(2,1,2)
# 画出函数曲线
plt.plot(x, y, color='g')
# 添加文字说明
plt.text(-3.0, 9, r'$y=ReLU(x)$', fontsize=13)
# 设置坐标轴格式
currentAxis=plt.gca()
currentAxis.xaxis.set_label_text('x', fontsize=15)
currentAxis.yaxis.set_label_text('y', fontsize=15)
plt.show()
【运行结果】
普通图
from matplotlib import pyplot as plt
import numpy as np
n = 256 #n可以理解为曲线的平滑程度
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()
【运行结果】
条形图
from matplotlib import pyplot as plt
#数据组1
x = [5,8,11] #横坐标
y = [12,16,6] #对应值
#数据组2
x2 = [6,9,12] #横坐标
y2 = [6,15,7] #对应值
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()
【运行结果】
散点图
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()
【运行结果】以下分别是方差分别为5,10,15,20,25,30的散点图
等高线图
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 = 256 #n可以理解为曲线的平滑程度
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()
【运行结果】
灰度图
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()
【运行结果】
饼状图
from matplotlib import pyplot as plt
import numpy as np
n = 20 #n决定了有多少个分区,该代码显示有20个分区
Z = np.random.uniform(0,1,n)
plt.pie(Z)
plt.show()
【运行结果】
量场图
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()
【运行结果】
极轴图
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()
【运行结果】
3D图
边栏推荐
- Procedure steps for burning a disc
- 带你解决哈希冲突,并实现一个简单hash表,
- Chen Qiang: Alibaba's 100 billion level large-scale digital business knowledge map helps business growth
- 如何创建并强制使用索引
- How to add an application to the deviceidle whitelist?
- VCD-影音光碟
- gdb安装
- Insert string B into string A. how many insertion methods can make the new string a palindrome string
- 解决pycharm里面每个字母占一格空格的问题
- Case study of row lock and isolation level
猜你喜欢

Boyun, standing at the forefront of China's container industry

CLion断点单步调试

Take you to resolve hash conflicts and implement a simple hash table,

交叉编译环境出现.so链接文件找不到问题

pycharm的plt.show()如何保持不关闭

Case study of row lock and isolation level

Paging query and join Association query optimization

next(iter(dataloader))的一点点体会

Tag dynamic programming - preliminary knowledge for question brushing -2 0-1 knapsack theory foundation and two-dimensional array solution template

in和exsits、count(*)查询优化
随机推荐
Let torch cuda. is_ Experience of available() changing from false to true
基于tensorflow的手写数字识别
Map and filter methods for processing scarce arrays
临时关闭MySQL缓存
JVM entry Door (1)
Binary search-2
Decision tree and random forest
股票开账户如何优惠开户?现在在线开户安全么?
How to open a stock account? Is it safe to open an account online now?
How about opening a flush account? Is it safe? How to open a stock trading account
CD-CompactDisk
非对称密码体制详解
PC端录制扫515地机器人/scan数据
MYSQL的下载与配置 mysql远程操控
Static registration and dynamic registration of JNI
wm_concat()和group_concat()函数
【NPOI】C#跨工作薄复制Sheet模板导出Excel
Concept and working principle of data encryption standard (DES)
RuntimeError: CUDA error: out of memory自己的解决方法(情况比较特殊估计对大部分人不适用)
Enter n integers and output the number of occurrences greater than or equal to half the length of the array