(1)共享单一绘图区域的坐标轴

'''
上一讲介绍了画布的划分,有时候想将多张图放在同一个绘图区域,
不想在每个绘图区域只绘制一幅图形,这时候借助共享坐标轴的方法实现在一个绘图区
绘制多幅图形的目的。
'''
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl mpl.rcParams["font.sans-serif"]=["SimHei"]
mpl.rcParams["axes.unicode_minus"]=False fig, ax1 = plt.subplots()
t = np.arange(0.05, 10, 0.01)
s1 = np.exp(t)
ax1.plot(t, s1, c="b", ls="-") ax1.set_xlabel("x坐标轴") ax1.set_ylabel("以e为底指数函数", color="r")
ax1.tick_params("y", color="b")
#将y轴标签,主刻度线和刻度标签设置
ax2 = ax1.twinx()
#实例ax2的主轴与实例ax1的x轴是共享的,实例ax2的刻度线和刻度标签在右侧轴脊处绘制
s2 = np.cos(t**2)
ax2.plot(t, s2, c="r", ls=":") ax2.set_ylabel("余弦函数", color="r")
ax2.tick_params("y", colors="r") plt.show()

(2)共享不同子区绘图区域的坐标轴

'''
共享不同子区绘图区域的坐标轴的方法是subplots(2, 2, sharey=True),
sharey=True是一区,二区共享y轴,还有其他参数,row,col,all,none,
其中all和none分别等同True和False。
'''
import matplotlib.pyplot as plt
import numpy as np x1 = np.linspace(0, 2*np.pi, 400)
y1 = np.cos(x1) x2 = np.linspace(0.01, 10, 100)
y2 = np.cos(x2) x3 = np.random.rand(100)
y3 = np.linspace(0, 3, 100) x4 = np.arange(0, 6, 0.5)
y4 = np.power(x4, 3) fig, ax = plt.subplots(2, 2)
#分成4个子区
ax1 = ax[0, 0]
ax1.plot(x1, y1)
#ax[0, 0]访问第一个子区
ax2 = ax[0, 1]
ax2.plot(x2, y2) ax3 = ax[1, 0]
ax3.scatter(x3, y3) ax4 = ax[1, 1]
ax4.plot(x4, y4) plt.show()

(3)将(2)中的plt.subplots(2, 2)改成plt.subplots(2, 2, sharex="all")-所有子区共享x轴

(4)参数sharex=“none”

与(2)相同

(5)参数sharex=“row”------->每一行x轴取值范围实现共享

(6)sharex="col"------------->每列共享x

(7)将共享坐标轴的子区之间的空隙去掉,似乎出了点问题,还是有空隙

import matplotlib.pyplot as plt
import numpy as np x = np.linspace(0.0, 10.0, 200)
y = np.cos(x)*np.sin(x)
y2 = np.exp(-x)*np.sin(x)
y3 = 3*np.sin(x)
y4 = np.power(x, 0.5) fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, sharex="all") fig.subplots_adjust(hspace=0) ax1.plot(x, y, ls="-", lw=2)
ax1.set_yticks(np.arange(-0.6, 0.7, 0.2))
ax1.set_ylim(-0.7, 0.7)

(8)共享个别子区绘图区域的坐标轴

import matplotlib.pyplot as plt
import numpy as np x1 = np.linspace(0, 2*np.pi, 400)
y1 = np.cos(x1**2) x2 = np.linspace(0.01, 10, 100)
y2 = np.sin(x2) x3 = np.random.rand(100)
y3 = np.linspace(0, 3, 100) x4 = np.arange(0, 6, 0.5)
y4 = np.power(x4, 3) fig, ax = plt.subplots(2, 2) ax1 = plt.subplot(221)
ax1.plot(x1, y1) ax2 = plt.subplot(222)
ax2.plot(x2, y2) ax3 = plt.subplot(223)
ax3.plot(x3, y3) ax4 = plt.subplot(224, sharex=ax1)
#与子区1共享x轴
ax4.plot(x4, y4) plt.show()

matplotlib学习日记(十)-共享绘图区域的坐标轴的更多相关文章

  1. 『Python』matplotlib共享绘图区域坐标轴

    1. 共享单一绘图区域的坐标轴 有时候,我们想将多张图形放在同一个绘图区域,不想在每个绘图区域只绘制一幅图形.这时候,就可以借助共享坐标轴的方法实现在一个绘图区域绘制多幅图形的目的. import n ...

  2. matplotlib学习日记(十)-划分画布的主要函数

    (1)函数subplot()绘制网格区域中的几何形状相同的子区布局 import matplotlib.pyplot as plt import numpy as np '''函数subplot的介绍 ...

  3. matplotlib学习日记(一)------图表组成元素

      1.使用函数绘制matplotlib的图表组成元素 (1)函数plot---变量的变化趋势 import matplotlib.pyplot as plt import numpy as np x ...

  4. matplotlib学习日记(十一)---坐标轴高阶应用

    (一)设置坐标轴的位置和展示形式 (1)向画布中任意位置添加任意数量的坐标轴 ''' 通过在画布的任意位置和区域,讲解设置坐标轴的位置和坐标轴的展示形式的实现方法, 与subplot,subplots ...

  5. matplotlib学习日记(二)----图表组成练习

    ''' 将前面的知识进行练习 plot,scatter,legend等 ''' import matplotlib.pyplot as plt import numpy as np from matp ...

  6. Python学习日记(十八) 序列化模块

    什么是序列? 就是每一个元素被有序的排成一列 什么是序列化? 就是将原本的列表.字典等内容转化成字符串的过程 什么时候会用到序列化? 数据存储(把数据放在文件.数据库),网络传输等 序列化的目的 1. ...

  7. matplotlib学习日记(九)-图形样式

    (一)刻度线定位器和刻度格式器的使用方法 import matplotlib.pyplot as plt import numpy as np from matplotlib.ticker impor ...

  8. matplotlib学习日记(八)----完善统计图

    (一)再说legend() import matplotlib.pyplot as plt import numpy as np x = np.arange(0, 2.1, 0.1) y = np.p ...

  9. matplotlib学习日记(七)---误差棒图

    (一)误差棒图----误差置信区间的表示 import matplotlib.pyplot as plt import numpy as np x = np.linspace(0.1, 0.6, 10 ...

随机推荐

  1. try...catch的前世今生

    1975年, John Goodenough发表论文,提出异常处理的建议: 一是明确声明命令可能抛出何种异常: 二是需要有将可能出错的操作括起来的语句结构. Communications of the ...

  2. 深度学习论文翻译解析(十五):Densely Connected Convolutional Networks

    论文标题:Densely Connected Convolutional Networks 论文作者:Gao Huang Zhuang Liu Laurens van der Maaten  Kili ...

  3. Python爬虫合集:花6k学习爬虫,终于知道爬虫能干嘛了

    爬虫Ⅰ:爬虫的基础知识 爬虫的基础知识使用实例.应用技巧.基本知识点总结和需要注意事项 爬虫初始: 爬虫: + Request + Scrapy 数据分析+机器学习 + numpy,pandas,ma ...

  4. EF Core 执行SQL语句和存储过程

    无论ORM有多么强大,总会出现一些特殊的情况,它无法满足我们的要求.在这篇文章中,我们介绍几种执行SQL的方法. 表结构 在具体内容开始之前,我们先简单说明一下要使用的表结构. public clas ...

  5. Mac MySQL 8.0 (免安装版) 主从集群搭建

    一.下载解压包 打开 MySQL 官网地址:https://dev.mysql.com/downloads/mysql/ ,选择面安装版本. 二.解压文件 下载到合适文件夹,解压压缩包. 解压 mys ...

  6. Spring Boot 集成多个 Kafka

    一.配置文件 application.yml spring: kafka: one: bootstrap-servers: IP:PORT consumer: group-id: YOUR_GROUP ...

  7. PyQt(Python+Qt)学习随笔:QDockWidget停靠部件的setTitleBarWidget方法

    setTitleBarWidget方法用于给停靠窗口设置个性化的标题栏,调用语法如下: setTitleBarWidget(QWidget widget) 说明: widget参数可以是任意一个QWi ...

  8. 第11.19节 Python 中正则表达式的扩展功能:前视断言和前视取反

    一. 引言 在<第11.16节 Python正则元字符"()"(小括号)与组(group)匹配模式>中老猿介绍了组匹配模式的命名组功能及引用组功能,这两者都是组模式的扩 ...

  9. Algorithm homework 1

    一.已知下列递推式: \[C(n)= \begin{cases} 1 & , & n = 1 \\ 2C(n/2) + n - 1& , & n \geq 2 \end ...

  10. 学生证申请Idea

    1.地址:https://www.jetbrains.com/shop/eform/students2.英语不好的一键翻译,说明:三五天下来3.上图: