matplotlib多图展示

import matplotlib.pyplot as plt

plt.figure(figsize=(6, 4))
# plt.subplot(n_rows, n_cols, plot_num)
plt.subplot(2, 2, 1)
plt.plot([0, 1], [0, 1])

plt.subplot(222)
plt.plot([0, 1], [0, 2])

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

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

plt.show()

import matplotlib.pyplot as plt

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

plt.subplot(2,3,4)
plt.plot([0,1],[0,2])

plt.subplot(235)
plt.plot([0,1],[0,4])

plt.subplot(236)
plt.plot([0,1],[0,5])

plt.show()

# subplot 分格显示

import matplotlib.pyplot as plt
import matplotlib.gridspec
plt.figure()
# colspan和rowspan缺省, 默认跨度为1.
ax1 = plt.subplot2grid((3,3),(0,0),colspan=3)
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),(1,2),rowspan=2)
ax4 = plt.subplot2grid((3,3),(2,0))
ax5 = plt.subplot2grid((3,3),(2,1))

ax4.scatter([1,2],[2,2])
ax4.set_xlabel('ax4_x')
ax4.set_ylabel('ax4_y')
# 紧凑显示图像
plt.tight_layout()
plt.show()

# 图中图 plot in plot 
import matplotlib.pyplot as plt

fig = plt.figure()

x = [1,2,3,4,5,6,7]
y = [1,3,4,2,5,8,6]

# 0.1, 0.1, 0.8, 0.8 都是占据整个plot的percent
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_xlabel('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')
ax2.set_title('title inside 1')

# 更简单的方式
plt.axes([0.6, 0.2, 0.25, 0.25])
plt.plot(y[::-1], x, 'g') # 注意对y进行了逆序处理
plt.xlabel('x')
plt.ylabel('y')
plt.title('title inside 2')
#b = a[i:j:s]这种格式呢,i,j与上面的一样,但s表示步进,缺省为1.
# 所以a[i:j:1]相当于a[i:j]
# 当s<0时,i缺省时,默认为-1. j缺省时,默认为-len(a)-1
# 所以a[::-1]相当于 a[-1:-len(a)-1:-1],也就是从最后一个元素到第一个元素复制一遍。所以你看到一个倒序的东东。

# 次坐标轴
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0,10,0.1)

y1 = 0.05 * x**2
y2 = -1*y1
fig, ax1 = plt.subplots()
# print(ax1)
# print(fig)
# 这里不是很懂为什么两个print输出的参数有一些不同

# ax2和ax1镜像
ax2 = ax1.twinx()

ax1.plot(x,y1,'g-')

ax1.set_xlabel('X data')
ax1.set_ylabel('Y1 data',color='g')

ax2.plot(x,y2,'b--')
ax2.set_ylabel('Y2 data',color='b')
Text(0,0.5,'Y2 data')

链接:https://nbviewer.jupyter.org/github/renhaofan/jupyfiles/blob/master/matplotlib3.ipynb

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于matplotlib排版,你可以使用subplot,gridspec或subplots来实现。 1. 使用subplot函数: subplot函数允许您在一个Figure对象中创建多个子。它接受三个参数:行数、列数和子索引。 以下是一个简单的例子,展示如何在一个Figure中创建2x2的子布局: ```python import matplotlib.pyplot as plt # 创建Figure对象,并设置子布局为2行2列 fig = plt.figure() # 创建子1,并设为第1个位置 ax1 = fig.add_subplot(2, 2, 1) ax1.plot([1, 2, 3, 4], [1, 4, 2, 3]) # 创建子2,并设为第2个位置 ax2 = fig.add_subplot(2, 2, 2) ax2.plot([1, 2, 3, 4], [4, 2, 3, 1]) # 创建子3,并设为第3个位置 ax3 = fig.add_subplot(2, 2, 3) ax3.plot([1, 2, 3, 4], [3, 1, 4, 2]) # 创建子4,并设为第4个位置 ax4 = fig.add_subplot(2, 2, 4) ax4.plot([1, 2, 3, 4], [2, 3, 1, 4]) # 显示形 plt.show() ``` 2. 使用gridspec模块: gridspec模块提供了更灵活的子布局选项。您可以使用gridspec.GridSpec将Figure分割成不规则的网格,并在每个网格中放置子。 以下是一个示例代码,展示如何使用gridspec实现2x2的子布局: ```python import matplotlib.pyplot as plt from matplotlib import gridspec # 创建Figure对象 fig = plt.figure() # 使用gridspec将Figure分割成2行2列 gs = gridspec.GridSpec(2, 2) # 在第1行第1列创建子 ax1 = fig.add_subplot(gs[0, 0]) ax1.plot([1, 2, 3, 4], [1, 4, 2, 3]) # 在第1行第2列创建子 ax2 = fig.add_subplot(gs[0, 1]) ax2.plot([1, 2, 3, 4], [4, 2, 3, 1]) # 在第2行第1列创建子 ax3 = fig.add_subplot(gs[1, 0]) ax3.plot([1, 2, 3, 4], [3, 1, 4, 2]) # 在第2行第2列创建子 ax4 = fig.add_subplot(gs[1, 1]) ax4.plot([1, 2, 3, 4], [2, 3, 1, 4]) # 显示形 plt.show() ``` 3. 使用subplots函数: subplots函数可以同时创建多个子,并返回一个包含所有子的Figure对象和Axes对象的数组。 以下是一个示例代码,展示如何使用subplots函数创建2x2的子布局: ```python import matplotlib.pyplot as plt # 使用subplots函数创建2x2的子布局 fig, axs = plt.subplots(2, 2) # 在第1行第1列创建子 axs[0, 0].plot([1, 2, 3, 4], [1, 4, 2, 3]) # 在第1行第2列创建子 axs[0, 1].plot([1, 2, 3, 4], [4, 2, 3, 1]) # 在第2行第1列创建子 axs[1, 0].plot([1, 2, 3, 4], [3, 1, 4, 2]) # 在第2行第2列创建子 axs[1, 1].plot([1, 2, 3, 4], [2, 3, 1, 4]) # 调整子之间的间距 plt.tight_layout() # 显示形 plt.show() ``` 这些是matplotlib中几种常见的多排版方法。您可以根据需要选择适合您的情况的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值