iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python+matplotlib绘制多子图的方法详解
  • 917
分享到

Python+matplotlib绘制多子图的方法详解

2024-04-02 19:04:59 917人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录本文速览1、matplotlib.pyplot api 方式添加子图2、面向对象方式添加子图3、matplotlib.pyplot add_subplot方式添加子图4、matp

本文速览

matplotlib.pyplot api 绘制子图

面向对象方式绘制子图

matplotlib.gridspec.GridSpec绘制子图

任意位置添加子图

关于pyplot和面向对象两种绘图方式可参考之前文章:matplotlib.pyplot api verus matplotlib object-oriented

1、matplotlib.pyplot api 方式添加子图

import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi,480/my_dpi),dpi=my_dpi)
plt.subplot(221)
plt.plot([1,2,3])


plt.subplot(222)
plt.bar([1,2,3],[4,5,6])
plt.title('plt.subplot(222)')#注意比较和上面面向对象方式的差异
plt.xlabel('set_xlabel')
plt.ylabel('set_ylabel',fontsize=15,color='g')#设置y轴刻度标签
plt.xlim(0,8)#设置x轴刻度范围
plt.xticks(range(0,10,2))   # 设置x轴刻度间距
plt.tick_params(axis='x', labelsize=20, rotation=45)#x轴标签旋转、字号等

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

plt.subplot(224)
plt.bar([1,2,3],[4,5,6])


plt.suptitle('matplotlib.pyplot api',color='r')
fig.tight_layout(rect=(0,0,1,0.9))




plt.subplots_adjust(left=0.125,
                    bottom=-0.51,
                    right=1.3,
                    top=0.88,
                    wspace=0.2,
                    hspace=0.2
                   )
                   

#plt.tight_layout()

plt.show()

2、面向对象方式添加子图

import matplotlib.pyplot as plt
my_dpi=96
fig, axs = plt.subplots(2,2,figsize=(480/my_dpi,480/my_dpi),dpi=my_dpi,
                       sharex=False,#x轴刻度值共享开启
                       sharey=False,#y轴刻度值共享关闭                        
                        
                       )
#fig为matplotlib.figure.Figure对象
#axs为matplotlib.axes.Axes,把fig分成2x2的子图
axs[0][0].plot([1,2,3])
axs[0][1].bar([1,2,3],[4,5,6])
axs[0][1].set(title='title')#设置axes及子图标题
axs[0][1].set_xlabel('set_xlabel',fontsize=15,color='g')#设置x轴刻度标签
axs[0][1].set_ylabel('set_ylabel',fontsize=15,color='g')#设置y轴刻度标签
axs[0][1].set_xlim(0,8)#设置x轴刻度范围
axs[0][1].set_xticks(range(0,10,2))   # 设置x轴刻度间距
axs[0][1].tick_params(axis='x', #可选'y','both'
                      labelsize=20, rotation=45)#x轴标签旋转、字号等


axs[1][0].plot([1,2,3])
axs[1][1].bar([1,2,3],[4,5,6])

fig.suptitle('matplotlib object-oriented',color='r')#设置fig即整整张图的标题

#修改子图在整个figure中的位置(上下左右)
plt.subplots_adjust(left=0.125,
                    bottom=-0.61,
                    right=1.3,#防止右边子图y轴标题与左边子图重叠
                    top=0.88,
                    wspace=0.2,
                    hspace=0.2
                   )

# 参数介绍
'''
## The figure subplot parameters.  All dimensions are a fraction of the figure width and height.
#figure.subplot.left:   0.125  # the left side of the subplots of the figure
#figure.subplot.right:  0.9    # the right side of the subplots of the figure
#figure.subplot.bottom: 0.11   # the bottom of the subplots of the figure
#figure.subplot.top:    0.88   # the top of the subplots of the figure
#figure.subplot.wspace: 0.2    # the amount of width reserved for space between subplots,
                               # expressed as a fraction of the average axis width
#figure.subplot.hspace: 0.2    # the amount of height reserved for space between subplots,
                               # expressed as a fraction of the average axis height


'''


plt.show()

3、matplotlib.pyplot add_subplot方式添加子图

my_dpi=96
fig = plt.figure(figsize=(480/my_dpi,480/my_dpi),dpi=my_dpi)
fig.add_subplot(221)
plt.plot([1,2,3])

fig.add_subplot(222)
plt.bar([1,2,3],[4,5,6])
plt.title('fig.add_subplot(222)')

fig.add_subplot(223)
plt.plot([1,2,3])

fig.add_subplot(224)
plt.bar([1,2,3],[4,5,6])
plt.suptitle('matplotlib.pyplot api:add_subplot',color='r')

4、matplotlib.gridspec.GridSpec方式添加子图

语法:matplotlib.gridspec.GridSpec(nrows, ncols, figure=None, left=None, bottom=None, right=None, top=None, wspace=None, hspace=None, width_ratiOS=None, height_ratios=None)

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec


fig = plt.figure(dpi=100,
                 constrained_layout=True,#类似于tight_layout,使得各子图之间的距离自动调整【类似excel中行宽根据内容自适应】
                 
                )

gs = GridSpec(3, 3, figure=fig)#GridSpec将fiure分为3行3列,每行三个axes,gs为一个matplotlib.gridspec.GridSpec对象,可灵活的切片figure
ax1 = fig.add_subplot(gs[0, 0:1])
plt.plot([1,2,3])
ax2 = fig.add_subplot(gs[0, 1:3])#gs[0, 0:3]中0选取figure的第一行,0:3选取figure第二列和第三列

#ax3 = fig.add_subplot(gs[1, 0:2])
plt.subplot(gs[1, 0:2])#同样可以使用基于pyplot api的方式
plt.scatter([1,2,3],[4,5,6],marker='*')

ax4 = fig.add_subplot(gs[1:3, 2:3])
plt.bar([1,2,3],[4,5,6])

ax5 = fig.add_subplot(gs[2, 0:1])
ax6 = fig.add_subplot(gs[2, 1:2])

fig.suptitle("GridSpec",color='r')
plt.show()

5、子图中绘制子图

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec


def fORMat_axes(fig):
    for i, ax in enumerate(fig.axes):
        ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
        ax.tick_params(labelbottom=False, labelleft=False)


# 子图中再绘制子图
fig = plt.figure(dpi=100,
                constrained_layout=True,
                )

gs0 = GridSpec(1, 2, figure=fig)#将figure切片为1行2列的两个子图

gs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[0])#将以上第一个子图gs0[0]再次切片为3行3列的9个axes
#gs0[0]子图自由切片
ax1 = fig.add_subplot(gs00[:-1, :])
ax2 = fig.add_subplot(gs00[-1, :-1])
ax3 = fig.add_subplot(gs00[-1, -1])

gs01 = gs0[1].subgridspec(3, 3)#将以上第二个子图gs0[1]再次切片为3行3列的axes
#gs0[1]子图自由切片
ax4 = fig.add_subplot(gs01[:, :-1])
ax5 = fig.add_subplot(gs01[:-1, -1])
ax6 = fig.add_subplot(gs01[-1, -1])

plt.suptitle("GridSpec Inside GridSpec",color='r')
format_axes(fig)

plt.show()

6、任意位置绘制子图(plt.axes)

plt.subplots(1,2,dpi=100)
plt.subplot(121)
plt.plot([1,2,3])


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



plt.axes([0.7, 0.2, 0.15, 0.15], ## [left, bottom, width, height]四个参数(fractions of figure)可以非常灵活的调节子图中子图的位置     
        )
plt.bar([1,2,3],[1,2,3],color=['r','b','g'])


plt.axes([0.2, 0.6, 0.15, 0.15], 
        )
plt.bar([1,2,3],[1,2,3],color=['r','b','g'])

以上就是python+matplotlib绘制多子图的方法详解的详细内容,更多关于Python matplotlib多子图的资料请关注编程网其它相关文章!

--结束END--

本文标题: Python+matplotlib绘制多子图的方法详解

本文链接: https://www.lsjlt.com/news/119126.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python+matplotlib绘制多子图的方法详解
    目录本文速览1、matplotlib.pyplot api 方式添加子图2、面向对象方式添加子图3、matplotlib.pyplot add_subplot方式添加子图4、matp...
    99+
    2024-04-02
  • Python Matplotlib如何绘制多子图
    这篇文章将为大家详细讲解有关Python Matplotlib如何绘制多子图,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。通过获取子图的label和线型来合并图例注意添加label#导入数据(读...
    99+
    2023-06-29
  • Matplotlib绘制子图的常见几种方法
    前言 Matplotlib的可以把很多张图画到一个显示界面,在作对比分析的时候非常有用。 对应的有plt的subplot和figure的add_subplo的方法,参数可以是一个三位...
    99+
    2024-04-02
  • PythonMatplotlib绘制多子图详解
    通过获取子图的label和线型来合并图例 注意添加label #导入数据(读者可忽略) pre_lp=total_res#组合模型 true=diff1[-pre_day:]#真实值...
    99+
    2024-04-02
  • Python matplotlib plotly绘制图表详解
    目录一、整理数据二、折线图三、散点图四、饼图五、柱形图六、点图(设置多个go对象)七、2D密度图八、简单3D图一、整理数据 以300部电影作为数据源 import pandas as...
    99+
    2024-04-02
  • Python matplotlib绘图详解
    目录图标英文显示设置:一、figure窗口及坐标轴设置  二、为特殊点加注解(Annotation)总结图标英文显示设置: 正常以字符串形式传进去字串,英文显示...
    99+
    2024-04-02
  • Python利用Matplotlib绘制图表详解
    目录前言折线图绘制与显示绘制数学函数图像散点图绘制绘制柱状图绘制直方图饼图前言 Matplotlib 是 Python 中类似 MATLAB 的绘图工具,如果您熟悉 MATLAB,那...
    99+
    2024-04-02
  • 详解Python+Matplotlib绘制面积图&热力图
    目录1.绘制面积图2.绘制热力图1.绘制面积图 面积图常用于描述某指标随时间的变化程度。其面积也通常可以有一定的含义。 绘制面积图使用的是plt.stackplot()方法。 以小学...
    99+
    2024-04-02
  • Python+Matplotlib绘制3D图像的示例详解
    目录1. 绘制3D柱状图2. 绘制3D曲面图示例1示例23.绘制3D散点图4. 绘制3D曲线图1. 绘制3D柱状图 绘制3D柱状图使用的是axes3d.bar()方法。 可能跟我们中...
    99+
    2024-04-02
  • Python绘图之详解matplotlib
    一、matplotlib介绍 matplotlib是python从matlab继承的绘图库,可以满足大部分的日常使用,是目前最流行的底层绘图库。 二、matplotlib的使用 (一...
    99+
    2024-04-02
  • python绘制饼图的方法详解
    用法 matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistan...
    99+
    2024-04-02
  • python——matplotlib绘图详解大全
    目录 一、图形绘制大全 1.1 2D图形绘制 1.1.1 绘制单线图形 1.1.2 绘制多线图 1.1.3 读取文件中的数据绘制图形 1.1.4 绘制散点图 1.1.5 绘制条形图 1.1.5.1 单条条形图 1、垂直条形图 2、水平条形...
    99+
    2023-09-09
    python matplotlib 开发语言
  • Python Matplotlib绘制箱线图boxplot()函数详解
    目录箱线图boxplot()函数还提供了丰富的自定义选项箱线图通常用在多组数据比较时补充:plt.boxplot()函数绘制箱图、常用方法实战常用方法总结 箱线图 箱线图一...
    99+
    2024-04-02
  • Python利用matplotlib绘制直方图
    目录1. 直方图概述 1.1什么是直方图? 1.2直方图使用场景 1.3直方图绘制步骤 1.4案例展示2. 直方图属性 2.1设置颜色 2.2设置长条形数目 2.3设置透明度 2.4...
    99+
    2024-04-02
  • 详解Python中matplotlib模块的绘图方式
    目录1、matplotlib之父简介2、matplotlib图形结构3、matplotlib两种画绘图方法方法一:使用matplotlib.pyplot方法二:面向对象方法1、mat...
    99+
    2024-04-02
  • python绘制棉棒图的方法详解
    目录案例参数总结用法: matplotlib.pyplot.stem(*args, linefmt=None, markerfmt=None, basefmt=None, botto...
    99+
    2024-04-02
  • python 用matplotlib绘制折线图详情
    目录1. 折线图概述 1.1什么是折线图? 1.2折线图使用场景 1.3绘制折线图步骤 1.4案例展示 2. 折线2D属性 2.1linestyle:折线样式 2.2color:折线...
    99+
    2024-04-02
  • Python+matplotlib绘制条形图和直方图
    目录摘要一、bar()函数二,hist()函数三、数据统计摘要 先介绍条形图直方图,然后用随机数生成一系列数据,保存到列表中,最后统计出相关随机数据的概率并展示 前述介绍了由点进行划...
    99+
    2024-04-02
  • 利用Matplotlib实现单画布绘制多个子图
    目录Matplotlib实现单画布绘制多个子图Matplotlib绘制多个动态子图总结Matplotlib实现单画布绘制多个子图 最近研究Python数据分析,需要利用Matplot...
    99+
    2023-02-07
    Matplotlib绘制多子图 Matplotlib单画布绘制子图 Matplotlib绘制多个子图
  • matplotlib图形整合之多个子图绘制的实例代码
    目录简单了解多子图使用plt.subplot(mnx) 分别绘制使用plt.subplots(m,n)一次性绘制高级进阶 总结简单了解多子图 学习matplotlib的时候...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作