文章目录

  • 1 Subplot多合一显示
  • 2 Subplot分格显示
    • 2.1 Subplot
    • 2.2 gridspec
    • 2.3 subplots
  • 3 图中图
  • 4 次坐标轴

1 Subplot多合一显示

import matplotlib.pyplot as pltplt.figure()plt.subplot(2,2,1)
plt.plot([0,1],[0,1])plt.subplot(2,2,2)
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()

不均匀图中图
如果希望展示的小图的大小不相同, 应该怎么做呢? 以上面的4个小图为例, 如果把第1个小图放到第一行, 而剩下的3个小图都放到第二行.

使用plt.subplot(2,1,1)将整个图像窗口分为2行1列, 当前位置为1. 使用plt.plot([0,1],[0,1])在第1个位置创建一个小图.

plt.subplot(2,1,1)
plt.plot([0,1],[0,1])
使用plt.subplot(2,3,4)将整个图像窗口分为2行3列, 当前位置为4. 使用plt.plot([0,1],[0,2])在第4个位置创建一个小图.

plt.subplot(2,3,4)
plt.plot([0,1],[0,2])
这里需要解释一下为什么第4个位置放第2个小图. 上一步中使用plt.subplot(2,1,1)将整个图像窗口分为2行1列, 第1个小图占用了第1个位置, 也就是整个第1行. 这一步中使用plt.subplot(2,3,4)将整个图像窗口分为2行3列, 于是整个图像窗口的第1行就变成了3列, 也就是成了3个位置, 于是第2行的第1个位置是整个图像窗口的第4个位置.

使用plt.subplot(235)将整个图像窗口分为2行3列,当前位置为5. 使用plt.plot([0,1],[0,3])在第5个位置创建一个小图. 同上, 再创建plt.subplot(236).

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

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

plt.show() # 展示

2 Subplot分格显示

2.1 Subplot

import matplotlib.pyplot as pltplt.figure()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.show()

2.2 gridspec

使用import导入matplotlib.pyplot模块, 并简写成plt. 使用import导入matplotlib.gridspec, 并简写成gridspec.

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
使用plt.figure()创建一个图像窗口, 使用gridspec.GridSpec将整个图像窗口分成3行3列.

plt.figure()
gs = gridspec.GridSpec(3, 3)
使用plt.subplot来作图, gs[0, :]表示这个图占第0行和所有列, gs[1, :2]表示这个图占第1行和第2列前的所有列, gs[1:, 2]表示这个图占第1行后的所有行和第2列, gs[-1, 0]表示这个图占倒数第1行和第0列, gs[-1, -2]表示这个图占倒数第1行和倒数第2列.

ax6 = plt.subplot(gs[0, :])
ax7 = plt.subplot(gs[1, :2])
ax8 = plt.subplot(gs[1:, 2])
ax9 = plt.subplot(gs[-1, 0])
ax10 = plt.subplot(gs[-1, -2])

2.3 subplots

使用plt.subplots建立一个2行2列的图像窗口,sharex=True表示共享x轴坐标, sharey=True表示共享y轴坐标. ((ax11, ax12), (ax13, ax14))表示第1行从左至右依次放ax11和ax12, 第2行从左至右依次放ax13和ax14.

f, ((ax11, ax12), (ax13, ax14)) = plt.subplots(2, 2, sharex=True, sharey=True)
使用ax11.scatter创建一个散点图.

ax11.scatter([1,2], [1,2])
plt.tight_layout()表示紧凑显示图像, plt.show()表示显示图像.

plt.tight_layout()
plt.show()

3 图中图

# 导入pyplot模块
import matplotlib.pyplot as plt# 初始化figure
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.8ax1 = 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([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')plt.show()


数据
首先是一些准备工作:

#导入pyplot模块
import matplotlib.pyplot as plt

#初始化figure
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
注意,4个值都是占整个figure坐标系的百分比。在这里,假设figure的大小是10x10,那么大图就被包含在由(1, 1)开始,宽8,高8的坐标系内。

将大图坐标系添加到figure中,颜色为r(red),取名为title:

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里添加新的坐标系:

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’)
最后显示图像:

plt.show()

4 次坐标轴

import matplotlib.pyplot as plt
import numpy as npx = np.arange(0, 10, 0.1)y1 = 0.05 * x**2y2 = -1 * y1fig, ax1 = plt.subplots()ax2 = ax1.twinx()ax1.plot(x, y1, 'g-')   # green, solid lineax1.set_xlabel('X data')ax1.set_ylabel('Y1 data', color='g')ax2.plot(x, y2, 'b-') # blueax2.set_ylabel('Y2 data', color='b')plt.show()


第一个y坐标
有时候我们会用到次坐标轴,即在同个图上有第2个y轴存在。同样可以用matplotlib做到,而且很简单。

首先,我们做一些准备工作:

import matplotlib.pyplot as plt
import numpy as np

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

y1 = 0.05 * x**2

y2 = -1 * y1
可以看到,y2和y1是互相倒置的。接着,获取figure默认的坐标系 ax1:

fig, ax1 = plt.subplots()

第二个y坐标
对ax1调用twinx()方法,生成如同镜面效果后的ax2:

ax2 = ax1.twinx()
接着进行绘图, 将 y1, y2 分别画在 ax1, ax2 上:

ax1.plot(x, y1, ‘g-’) # green, solid line

ax1.set_xlabel(‘X data’)

ax1.set_ylabel(‘Y1 data’, color=‘g’)

ax2.plot(x, y2, ‘b-’) # blue

ax2.set_ylabel(‘Y2 data’, color=‘b’)

显示图像:

plt.show()

python【Matlibplot绘图库】多图合并显示(真の能看懂~!)相关推荐

  1. python【Matlibplot绘图库】画图种类(真の能看懂~!)

    文章目录 1 Scatter散点图 2 Bar柱状图 3 Contours 等高线图 4 Image 图片 5 3D 数据 1 Scatter散点图 import matplotlib.pyplot ...

  2. python【Matlibplot绘图库】Animation动画(真の能看懂~!)

    文章目录 1 代码 2 效果 3 解释 1 代码 from matplotlib import pyplot as plt from matplotlib import animation impor ...

  3. python【Matlibplot绘图库】基本使用(真の能看懂~!)

    文章目录 1 基本用法 2 figure图像 3 设置坐标轴1 4 设置坐标轴2 5 Legend图例 6 Annotation标注 7 tick能见度 1 基本用法 import matplotli ...

  4. python【Matlibplot绘图库】-主要概念

    文章目录 1.概述 2.各函数含义 3.numpy处理数据 1.概述 Matplotlib的GitHub链接: https://github.com/matplotlib/matplotlibMatp ...

  5. python 三维绘图库_Python第三方库matplotlib(2D绘图库)入门与进阶

    Matplotlib 一 简介: Matplotlib是一个Python 2D绘图库,它可以在各种平台上以各种硬拷贝格式和交互式环境生成出具有出版品质的图形. Matplotlib可用于Python脚 ...

  6. python安装绘图库matplotlib_Python基础教程:Python 2D绘图库 Matplotlib 简介和安装

    原标题:Python基础教程:Python 2D绘图库 Matplotlib 简介和安装 来自:Linux迷https://www.linuxmi.com/python-2d-matplotlib.h ...

  7. python画图程序没有图_解决python中使用plot画图,图不显示的问题

    解决python中使用plot画图,图不显示的问题 对以下数据画图结果图不显示,修改过程如下 df3 = {'chinese':109, 'American':88, 'German': 66, 'K ...

  8. python【Matlibplot绘图库】利用matlibplot绘制雷达图

    文章目录 1.基本构造 2.比较功能 1.基本构造 之前在一些数据分析案例中看到用 Go 语言绘制的雷达图,非常的漂亮,就想着用matlibplot.pyplot也照着画一个,遗憾的是matlibpl ...

  9. python 3d绘图库_python – 用于科学3d绘图的Mayavi的替代品

    在没有令人满意的第一个答案和无法解释的downvote之后编辑: 我需要绘制一个在3D网格中构造的标量字段,如下所示: import numpy as np from mayavi import ml ...

  10. python绘图库seaborn_Matplotlib Toolkits:python高级绘图库seaborn

    Seaborn介绍 seaborn (Not distributed with matplotlib) seaborn is a highlevel interface for drawing sta ...

最新文章

  1. WIN10映射ubuntu1604共享目录(网络驱动器)
  2. Effective C++ 读书笔记(八)
  3. 【收集】Python 微优化
  4. 计算机办公应用软件初级,电脑办公软件有哪些?桌面便签办公软件基础教程
  5. EmEditor18.1.2 注册码
  6. linux内核中TCP发送的实现
  7. IOS数据本地存储的四种方式--
  8. Boost升压电路调试
  9. 全面拆解携程云原生实践,打造智能弹性的云端酒店直连系统
  10. 在桌面计算机找不到光盘驱动,如何弹出DVD驱动器,没有按钮,我在计算机中找不到DVD驱动器...
  11. 百度地图使用之基本功能
  12. 游戏服务器没有响应怎么回事,求助!!!为什么我进不了游戏!!!
  13. 关于F# 6的那些新功能?你了解吗?
  14. Coding:用指针的方法,将字符串首尾对调输出
  15. 什么是STAR原则?
  16. 雷军狂送20亿给员工:网络工程师怎样才能最快体验到大厂待遇?
  17. 字母,数字,下划线,数字的正则表达式
  18. 100美元哪里去了?
  19. 离散数学—命题逻辑知识点整理
  20. Linux查看占用内存最高的进程

热门文章

  1. 最轻快的人脸检测yoloface
  2. win10 右键 命令行
  3. ProxylessNAS pytorch
  4. python对应位置相乘
  5. TensorFlow tf.nn.conv2d是怎样实现卷积的?
  6. 特征点检测学习_2(surf算法)
  7. 数据标准化(归一化)
  8. 青龙羊毛——狸猫十堰
  9. html语言技术基础,第2章Web编程基础HTML语言技术方案.ppt
  10. android设置光标闪烁,Android EditText闪烁光标