Matplotlib绘图工具(四)

1、subplot多图和为一图显示

# 创建一个绘图窗口
plt.figure()

# 给定位置描述,图像分为2行2列,第一个位置显示某张图片
plt.subplot(2,2,1)
plt.plot([0,1], [0,1])


# 第二个位置显示某张图片
plt.subplot(222)
plt.plot([0,1], [0,1])

plt.subplot(223)
plt.plot([0,1], [0,1])

plt.subplot(224)
plt.plot([0,1], [0,1])

plt.show()

2、subplot分格显示

import matplotlib.gridspec as gridspec

plt.figure()

# 方法一subplot2grid,分成很多的ax,整个gride有3行3列,开始位置,跨度
ax1 = plt.subplot2grid((3,3),(0,0),colspan = 3, rowspan = 1)      # 图的位置
ax1.plot([1,2], [1,2])
ax1.set_title('ax1_title')
ax2 = plt.subplot2grid((3,3),(1,0),colspan = 2,)
ax3 = plt.subplot2grid((3,3),(2,0),rowspan = 2)
ax4 = plt.subplot2grid((3,3),(2,0))
ax5 = plt.subplot2grid((3,3),(2,1))


# 方法二:导入新包gridspec

plt.figure()
gs = gridespec.GridSpec(3,3)
ax1 = plt.subplot(gs[0,:])
# 到第二个截止
ax2 = plt.subplot(gs[1,:2])
# 1之后的全部
ax3 = plt.subplot(gs[1:,2])
ax4 = plt.subplot(gs[-1,0])
ax5 = plt.subplot(gs[-1,-2])


# 方法三:easy to define structure,返回值为图片,所有图像的格式,第一行所有axis,第二行所有axis
f, ((ax11,ax12), (ax21,ax22)) = plt.subplots(2,2,sharex = True, sharey = True)
ax11.scatter([1,2],[1,2])

plt.show()

3、图中图

fig = plt.figure()

# 绘制大图,图像的左右宽高为多少,百分比
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
ax1 = fig.add_axes([left, bottom, width, height])
ax1.plot(x,y,'r')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_title('title')


left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
ax2 = fig.add_axes([left, bottom, width, height])
ax2.plot(y,x,'b')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_title('title inside 1')

plt.axes([.6,0.2,0.25,0.25])
plt.plot(y[::-1],x,'g')
plt.xlabel('x')
plt.ylabel('y')
plt.title('title inside 2')

4、建立次坐标轴,共享同一个x轴

fig,ax1 = plt.subplots()


# 镜面反射该轴数据ax2的坐标轴是ax1的坐标轴反向的
ax2 = ax1.twinx()
ax1.plot(x,y1,'g-')
ax2.plot(x,y2,'b--')

ax1.set_xlabel('x data')
ax1.set_ylabel('y1', color = 'g')
ax2.set_ylabel('y2', color = 'b')

plt.show()

5.animation动画

interval隔多少毫秒更新一次,更新整张图的值,

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

。七十二。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值