Matplotlib 入门(三):多图合并

2023-05-16

一、多合一显示

   1、subplot方法:设置行、列和起始点

plt.subplot(2,1,1)      # 分成两行一列,起始点为1

   2、代码

# -*- coding: utf-8 -*-
"""
Created on Sun Sep 24 15:02:51 2017

@author: ryoyun
"""

# subplot 多合一显示
import matplotlib.pyplot as plt

plt.figure()

plt.subplot(2,1,1)      # 分成两行一列,起始点为1
plt.plot([0,1],[0,1])   # 设置xy轴范围

plt.subplot(2,3,4)      # 分成两行三列,起始点位4
plt.plot([0,1],[0,2])

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

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

plt.show()

   3、效果

二、分格显示

   1、代码

# -*- coding: utf-8 -*-
"""
Created on Sun Sep 24 15:11:01 2017

@author: ryoyun
"""

# 分格显示
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

# method 1: subplot2grid
############################
#plt.figure()
#ax1 = plt.subplot2grid((3,3),(0,0),colspan = 3,rowspan = 1)
#ax1.plot([1,2],[1,2])
#ax1.set_title('ax1_title')

#plt.figure()
#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))

# method 2: gridspec
###########################
#plt.figure()
#gs = gridspec.GridSpec(3,3)
#ax1 = plt.subplot(gs[0,:])
#ax2 = plt.subplot(gs[1,:2])
#ax3 = plt.subplot(gs[1:,2])
#ax4 = plt.subplot(gs[-1,0])
#ax5 = plt.subplot(gs[-1,-2])


# method 3: easy to define structure
###########################
f,((ax11,ax12),(ax21,ax22)) = plt.subplots(2,2,sharex=True,sharey=True)
ax11.scatter([1,2],[1,2])


plt.tick_layout()
plt.show()

   2、效果

三、图中图

   1、每个图的位置设定很重要

left,bottom,width,height = 0.1,0.1,0.8,0.8
ax1 = fig.add_axes([left,bottom,width,height])# 设置位置

   2、代码

# -*- coding: utf-8 -*-
"""
Created on Sun Sep 24 15:32:13 2017

@author: ryoyun
"""

# 图中图
import matplotlib.pyplot as plt
import numpy as np  

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,1.5,1.5
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')


plt.show()

   3、效果

四、次坐标轴(两个Y轴)

   1、画几个散点一点都不难

ax2 = ax1.twinx()           # 做镜像处理,ax2是ax1的镜像

   2、代码

# -*- coding: utf-8 -*-
"""
Created on Sun Sep 24 15:44:29 2017

@author: ryoyun
"""

# 次坐标轴
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()
ax2 = ax1.twinx()           # 做镜像处理
ax1.plot(x,y1,'g-')
ax2.plot(x,y2,'b--')

ax1.set_xlabel('X data')    #设置x轴标题
ax1.set_ylabel('Y1',color = 'g')   #设置Y1轴标题
ax2.set_ylabel('Y2',color = 'b')   #设置Y2轴标题

plt.show()

   3、效果

五、Animation动画

   1、代码

# -*- coding: utf-8 -*-
"""
Created on Sun Sep 24 15:51:25 2017

@author: ryoyun
"""

# animation 动画
from matplotlib import animation
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

x = np.arange(0,2*np.pi,0.01)
line, = ax.plot(x,np.sin(x))

def animate(i):
    line.set_ydata(np.sin(x+i/100))
    return line,

def init():
    line.set_ydata(np.sin(x))
    return line,

ani = animation.FuncAnimation(fig =fig,func=animate,frames= 100,init_func=init,
                              interval=20,blit=False,)


plt.show()

   2、效果

参考:莫烦python                 https://morvanzhou.github.io/tutorials/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Matplotlib 入门(三):多图合并 的相关文章

随机推荐

  • java开发的正常开发步骤和进度总结

    项目规范 需求排期阶段 1 业务提需求 xff0c 业务与PD和技术初步探讨业务背景和需求 在此阶段 xff0c 开发人员的目标是提前的了解需求 xff0c 想业务之所想 xff0c 丰富和优化需求 所以 xff0c 开发人员 xff0c
  • 降级、熔断和限流———一看就会

    设定 xff1a A上游系统 B本系统 C下游系统 服务降级 服务降级是从整个系统B的负荷情况出发和考虑的 xff0c 对某些负荷会比较高的情况 xff0c 为了预防某些功能 xff08 业务场景 xff09 出现负荷过载或者响应慢的情况
  • 高保链路分析——一看就会

    高保链路分析 本身接口 超时降级 调用本接口耗时超过规定时间立马返回超时报文 限流降级 调用本接口QPS超过规定数值立马返回限流报文 兜底返回 xff08 代码容错 xff09 trycatch未知异常 xff0c 兜底返回报文 业务逻辑
  • android应用层操作底层硬件

    app操作底层硬件没权限的解决办法 xff1a 1 若机器已经root过 xff0c 可直接在应用层中操作 xff1a String apkRoot 61 34 chmod 777 34 43 getPackageCodePath Syst
  • ARM 安装中文输入法

    TX2 为嵌入式开发板 xff0c 系统架构为ARM xff0c 普通的PC上的Linux系统安装中文输入法的方法不适用 xff0c 所以这里提供ARM上的中文输入法的安装方法 步骤1 xff1a 打开终端 xff0c 输入以下命令 sud
  • cordova 打包步骤

    年底了 xff0c 好多资料都在整理 xff0c 为了避免遗忘 xff0c 也为了利益他人吧 直接上步骤吧 xff1a 增加运行环境的模板 cordova platform add android 编译android的程序 cordova
  • BlueROV-7: Keep Learning Dronekit

    The motors can spin now by using the following program from dronekit import connect VehicleMode import sys import time C
  • BlueROV-8: Functions to Drive the Vehicle

    Some functions like goto function or speed setting function are limited because they are GPS dependent GPS is not availa
  • BlueROV-9: Driving Control

    Home location http python dronekit io guide vehicle state and parameters html The Home location isset when a vehicle fir
  • 人工智能发展简史

    人工智能发展简史 第一章 xff1a 起步期 20世纪50年代及以前1 1 计算机象棋博弈 xff08 Programming a computer for playing chess xff09 1 2 图灵测试 xff08 Turing
  • 对角度滤波时0-360度跳变的解决办法

    对角度滤波的过程中会发现 xff0c 视觉direction方向会发生0 360的数值跳变 xff0c 为了解决这个问题 xff0c 需要将0和360度之间的间断点变成连续可导的函数 xff0c 经过摸索 xff0c 想出一个办法 xff0
  • 电机控制 龙伯格观测器 永磁同步电机无传感器控制 全C代码程序

    电机控制 龙伯格观测器 永磁同步电机无传感器控制 全C代码程序 成熟产品方案 DSP28335 xff08 1 xff09 全C程序完成由电机参数 电流微分方程构建dq轴误差模型 控制参数逻辑变换 低通滤波器转速滤波和转子角度积分等控制环节
  • ROS学习笔记-1: 构建工作空间-创建catkin包-编写发布器与订阅器

    1 ROS文件系统介绍 http wiki ros org cn ROS Tutorials NavigatingTheFilesystem 2 Installing catkin http wiki ros org catkin Inst
  • 信息安全重点知识

    一 信息安全概述 网络空间安全的重要性 xff1a 没有网络安全就没有国家安全信息安全 xff1a 防止数据未授权的访问 xff0c 数据有意和物一的威胁 网络安全是信息安全的子集 信息安全的三要素 xff08 CIA xff09 xff1
  • 海天注塑机KEBA系统数据采集

    本文章只针对海天注塑机的KEBA系统 xff0c 因为其他注塑机厂家也用KEBA系统 xff0c 他们的采集方式可能不太一样 xff0c 所以后续有时间我将写其他文章来解释 xff08 默认你已经向海天采购了OPC组件 xff09 一 采集
  • Axure基础:事件和动态面板

    这一篇文章我们主要是将如何做系统左侧的导航 xff0c 并且告诉大家如何动态的切换各个页面 一 事件 1 事件基础 事件的核心就是什么时候做什么事 其中的什么时候可以是如下 xff1a 能做的事情如下 xff1a 2 远程监控云中的事件 监
  • 设备联网调试三板斧

    在实际的工业互联网项目中 xff0c 设备联网所占的比重越来越大 有的一期项目为了简单快速上线 xff0c 让客户直观体会到工业互联网的效果 xff0c 直接会把设备联网放在一期项目的重点 那么在做此类项目时 xff0c 设备联网调试就显得
  • 光立方完全解析

    转载请注明出处 xff1a http blog csdn net ruoyunliufeng article details 37903899 这个4 4 4的三色光立方是我在初学单片机的时候做的一个小项目 很适合给初学单片机和C语言的同学
  • 远程视频监控之应用篇(mjpg-streamer)

    转载请注明出处 xff1a http blog csdn net ruoyunliufeng article details 38515311 这篇文章将主要结合源码介绍mjpg streamer xff0c 使小伙伴们了解视频监控的实现
  • Matplotlib 入门(三):多图合并

    一 多合一显示 1 subplot方法 xff1a 设置行 列和起始点 plt subplot 2 1 1 分成两行一列 xff0c 起始点为1 2 代码 coding utf 8 34 34 34 Created on Sun Sep 2