matplotlib共享坐标轴的实现(X或Y坐标轴)

作者:EWBA_GIS_RS_ER 时间:2023-12-01 23:58:12 

前言  

1. 概述

共享坐标轴就是几幅子图之间共享x轴或y轴,这一部分主要了解如何在利用matplotlib制图时共享坐标轴。


pyplot.subplots(nrows = 1,ncols = 1,sharex = False,sharey = False,
squeeze = True,subplot_kw =无,gridspec_kw =无,** fig_kw )

参数:
nrows:行数
ncols:列数
sharex:是否共享X轴坐标
sharey:是否共享Y轴坐标
返回值:Figure,Axes对象数组

一、sharex和sharey 代码示例:


'''
1. 程序目的:
  基于sharex和sharey实现
    (1) 共享x轴
    (2) 共享y轴
    (3) 同时共享x轴和y轴
    (4) 调整子图之间的距离

2. 版本
  2.1 山东青岛  2021年5月18日  Version 1

'''

# 1. 相关模块导入
import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei'] # 正常显示中文字体
plt.rcParams['axes.unicode_minus'] = False  # 正常显示负号

# 2. 创建制图数据
x = np.linspace(-5,5,100)
y_1 = np.sin(x)
y_2 = np.cos(x)
y_3 = y_2*2

# 3. 绘图
 # 3.1 共享X轴
figure,(ax1,ax2,ax3) = plt.subplots(3,1,
                                   figsize=(5,6),
                                   dpi=600,
                                   # 共享x轴
                                   sharex=True)

ax1.plot(x,y_1,c='blue',linestyle=':')
ax2.plot(x,y_2,c='orange',linestyle=':')
ax3.plot(x,y_3,c='r',linestyle=':')

# 调整子图形之间的纵向距离
figure.subplots_adjust(hspace=0.1)

ax1.set_title('以下三图共享了X轴')  # 其实更合理的添加图名时figure.subtitle()

# 3.2 共享Y轴
   # 创建新的绘图figure和axes对象
figure,(ax1,ax2,ax3) = plt.subplots(1,3,
                                   figsize=(6,2),
                                   dpi=600,
                                   # 共享y轴
                                   sharey=True)
figure.suptitle('以下三图共享了Y轴')
ax1.plot(x,y_1,c='blue',linestyle=':')
ax2.plot(x,y_2,c='orange',linestyle=':')
ax3.plot(x,y_3,c='r',linestyle=':')
   # 调整子图形之间的横向距离
figure.subplots_adjust(wspace=0.1)

# 3.3 同时共享x轴和y轴
   # 创建新的绘图figure和axes对象
figure,(ax1,ax2,ax3) = plt.subplots(1,3,
                                   figsize=(6,2),
                                   dpi=600,
                                   # 共享x轴
                                   sharex=True,
                                   # 共享y轴
                                   sharey=True)

x4 = np.linspace(-10,10,100)
y_4 = np.cos(x4)*2

figure.suptitle('以下三图同时共享了X轴和Y轴')
ax1.plot(x,y_1,c='blue',linestyle=':')
ax2.plot(x,y_2,c='orange',linestyle=':')
ax3.plot(x4,y_4,c='r',linestyle=':')

# 调整子图形之间的横向距离
figure.subplots_adjust(wspace=0.1)

plt.show()

制图结果:

matplotlib共享坐标轴的实现(X或Y坐标轴)

matplotlib共享坐标轴的实现(X或Y坐标轴)

matplotlib共享坐标轴的实现(X或Y坐标轴)

实例2

matplotlib共享坐标轴的实现(X或Y坐标轴)


import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(0,2*np.pi,500)
y=np.sin(x)*np.exp(-x)
fig,ax=plt.subplots(nrows=1,ncols=2,sharey=True)
ax1=ax[0]
ax1.plot(x,y)
ax1.set_title("折线图")

ax2=ax[1]
ax2.scatter(x,y)
ax2.set_title("散点图")
plt.suptitle("一张画布两个子图,并共享y坐标")
#删除空隙wspace为两图的水平距离,hspace为两图的垂直距离
fig.subplots_adjust(wspace=0)
plt.show()

matplotlib共享坐标轴的实现(X或Y坐标轴)


import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(0,2*np.pi,500)
y=np.sin(x)*np.exp(-x)
fig,ax=plt.subplots(nrows=1,ncols=1)
ax.plot(x,y)
ax.set_title("折线图")
ax.scatter(x,y[::-1])
plt.suptitle("共享单一绘图区域的坐标轴")
plt.show()

来源:https://blog.csdn.net/EWBA_GIS_RS_ER/article/details/117000162

标签:matplotlib,共享坐标轴
0
投稿

猜你喜欢

  • python使用技巧-查找文件 

    2021-11-10 18:48:21
  • spring boot整合mybatis使用c3p0数据源连接mysql

    2024-01-25 08:13:15
  • python系统指定文件的查找只输出目录下所有文件及文件夹

    2021-03-23 08:00:04
  • python+opencv像素的加减和加权操作的实现

    2021-11-29 03:39:14
  • golang实现单点登录系统(go-sso)

    2024-02-20 11:52:45
  • 利用nohup来开启python文件的方法

    2023-05-08 07:51:29
  • python 代码运行时间获取方式详解

    2021-01-22 19:07:36
  • vue中的ref和$refs的使用

    2024-05-13 09:08:44
  • 使用scrapy ImagesPipeline爬取图片资源的示例代码

    2023-07-07 13:46:05
  • MySQL系列之六 用户与授权

    2024-01-28 05:37:31
  • Python filter过滤器原理及实例应用

    2021-03-20 13:11:13
  • Python实现向好友发送微信消息优化篇

    2022-02-18 18:07:08
  • python简单实现9宫格图片实例

    2021-10-22 13:23:25
  • python正则表达式re.match()匹配多个字符方法的实现

    2023-07-30 08:25:16
  • 基于python if 判断选择结构的实例详解

    2023-01-15 22:45:03
  • thinkPHP中配置的读取与C方法详解

    2023-11-14 17:12:35
  • MySQL关系型数据库事务的ACID特性与实现

    2024-01-21 13:15:17
  • PHP中重启php-fpm的几种方法汇总

    2023-06-12 21:05:24
  • 大家都来设计创意XP黑屏!

    2008-10-25 14:59:00
  • Python3 适合初学者学习的银行账户登录系统实例

    2021-06-16 05:09:22
  • asp之家 网络编程 m.aspxhome.com