Python matplotlib可视化分析(五)- subplot gridspec subplots twinx twiny

对Python matplotlib 的使用进行了总结注释,学习内容来自B站up主莫烦Python,推荐!!!

代码:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
# subplot子图  多合一显示
plt.figure()
plt.subplot(2, 2, 1) # 把图分成两行两列,这个图放在第一个位置
plt.plot([0, 1], [0, 1])

# 第二张图
plt.subplot(2, 2, 2) # 把图分成两行两列,这个图放在第二个位置
plt.plot([0, 1], [0, 1])
# 第三张图
plt.subplot(2, 2, 3)
plt.plot([0, 1], [0, 2])
# 第四张图
plt.subplot(2, 2, 4)
plt.plot([0, 1], [0, 1])

plt.show()

运行结果:

# subplot子图 第一行一个图,第二行多个图
plt.figure()
plt.subplot(2, 1, 1)
plt.plot([0, 1], [0, 1])

# 第二张图
plt.subplot(2, 3, 4)  # 第二行起始位置为4
plt.plot([0, 1], [0, 1])
# 第三张图
plt.subplot(2, 3, 5)
plt.plot([0, 1], [0, 5])
# 第三张图
plt.subplot(2, 3, 6)
plt.plot([0, 1], [0, 2])

plt.show()

运行结果:

# subplot 分格显示
# method 1: subplot2grid
plt.figure()
# 整个图有3行3列,从(0,0)点开始画,行跨度colspan为3,列跨度rowspan为1
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3, rowspan=1)
ax1.plot([1, 2], [1, 2]) #两个点
ax1.set_title('ax1_title')
# ax2 从(1,0)点开始画,下标从0开始
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), colspan=1,rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))  # colspan,rowspan 默认取值为1
ax5 = plt.subplot2grid((3, 3), (2, 1))

ax2.plot(np.linspace(-5, 5, 100), np.sin(np.linspace(-5, 5, 100)))
ax3.plot(np.linspace(-3, 3, 50), np.tan(np.linspace(-3, 3, 50)))
ax4.plot(np.linspace(-3, 3, 50), np.cos(np.linspace(-3, 3, 50)))
ax5.plot(np.linspace(-3, 3, 50), np.tanh(np.linspace(-3, 3, 50)))
plt.show()

输出结果:

# subplot 分格显示
# method 2 gridspec
plt.figure()
gs = gridspec.GridSpec(3, 3)  # 3行3列的GridSpec
ax1 = plt.subplot(gs[0, :])  #ax1在GridSpec中是第0行,所有列
ax2 = plt.subplot(gs[1, :2])  #ax2在GridSpec中是第1行,第0、1列
ax3 = plt.subplot(gs[1:, 2])
ax4 = plt.subplot(gs[2, 0])
ax5 = plt.subplot(gs[2, 1])
plt.show()

输出结果(没有plot):

# subplot 分格显示
# method 3
# 两行两列,是否共享X轴,是否共享Y轴
fig, ((ax11, ax12), (ax21, ax22)) = plt.subplots(2, 2, sharex=True, sharey=True)
ax11.scatter([1, 2], [1, 2])
ax12.plot(np.linspace(-3, 3, 50), np.tan(np.linspace(-3, 3, 50)))
ax21.plot(np.linspace(-3, 3, 50), np.tan(np.linspace(-3, 3, 50)))
ax22.plot(np.linspace(-3, 3, 50), np.tan(np.linspace(-3, 3, 50)))

fig.suptitle('This is subplots!', x=0.5, y=1)  # fontsize  x,y位置

plt.show()

# 图中图
fig = plt.figure()
x = [1, 2, 3, 4, 5, 6, 7]
y = [1, 3, 4, 2, 5, 8, 6]

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_title('title')
ax1.set_xlabel('x')
ax1.set_ylabel('y')

left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
ax2 = fig.add_axes([left, bottom, width, height])  # 位置 宽度 高度(值是百分比)
ax2.plot([1, 2], [2, 5], 'b')
ax2.set_title('title inside1')
ax2.set_xlabel('x')
ax2.set_ylabel('y')

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

plt.show()

运行结果:

# 主次坐标轴,共享x轴,y轴数据不同
x = np.arange(0, 10, 0.1)
y1 = 0.05*x**2
y2 = -1*y1

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()    # y坐标轴twin
# ax2 = ax1.twiny()  # x坐标轴twin
ax1.plot(x, y1, 'g-')
ax2.plot(x, y2, 'b-')

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

运行结果:

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值