在Matplotlib的Figure中绘制多个Axes的两种方式

使用subplots

import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(2,3)
ax00 = axs[0][0]
ax01 = axs[0][1]
ax02 = axs[0][2]
ax10 = axs[1][0]
ax11 = axs[1][1]
ax12 = axs[1][2]
x = np.linspace(0,2*np.pi,100)
y_sin00 = np.sin(x)
y_sin01 = np.sin(x)
y_sin02 = np.sin(x)
y_sin10 = np.sin(x)
y_sin11 = np.sin(x)
y_sin12 = np.sin(x)
ax00.plot(x,y_sin00) 
ax01.plot(x,y_sin01) 
ax02.plot(x,y_sin02) 
ax10.plot(x,y_sin10) 
ax11.plot(x,y_sin11)
ax12.plot(x,y_sin12) 
plt.show()

在这里插入图片描述

使用subplot

import numpy as np
import matplotlib.pyplot as plt


x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)

y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)

fig = plt.figure()

plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'p-',label = '1')
plt.title('A tale of 2 subplots')
plt.ylabel('Damped oscillation')
plt.gca().legend()
A = plt.gca()

plt.subplot(2, 1, 2)
plt.plot(x2, y2, '.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')
# B = plt.gca()

#使用如下方法也可以在一个Figure中添加多个axes
#ax = fig.add_subplot(212)
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值