栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

使用matplotlib库中的GridSpec()来制定所需的网格,使用subplot()自定义图片位置。

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用matplotlib库中的GridSpec()来制定所需的网格,使用subplot()自定义图片位置。

今天来教大家画下图不规则的图片。
理论思想:首先可以从图中初步看出它是一个3*3的网格,使用mp.GridSpec(3, 3)来定义3*3的网格,再使用mp.subplot(gs[0, :2])来展示网格中x轴为0,y轴为0、1即图中数字1的位置,依此展示1到5这些数字的位置。


导入所需要的matplotlib库
import matplotlib.pyplot as mp

定义画布的名称
mp.figure("subplot", facecolor='lightgray')

定义网格大小
gs = mp.GridSpec(3, 3)

使用mp.subplot来依此展示1~5的数字图
mp.subplot(gs[0, :2])#数字1的位置是x轴为0 y轴为0 1
mp.text(0.5, 0.5, 1, size=36,
        ha='center', va='center')

mp.subplot(gs[0:2, 2])#数字2的位置是x轴为0 1 y轴为0 2 
mp.text(0.5, 0.5, 2, size=36, 
        ha='center', va='center')

mp.subplot(gs[2, 1:])#数字3的位置是x轴为2 y轴为1 2
mp.text(0.5, 0.5, 3, size=36,
        ha='center', va='center')

mp.subplot(gs[1:, 0])#数字4的位置是x轴为1 2 y轴为0
mp.text(0.5, 0.5, 4, size=36,
        ha='center', va='center')

mp.subplot(gs[1, 1])#数字5的位置是x轴为1 y轴为 1
mp.text(0.5, 0.5, 5, size=36,
        ha='center', va='center')

将图片展示出来
mp.show()

通过简单的几行代码即可自定义展示所需的图片了,如果需要修改展示的内容可将展示的内容与对应的mp.text()替换

全部代码如下:

import matplotlib.pyplot as mp

mp.figure("subplot", facecolor='lightgray')

gs = mp.GridSpec(3, 3)
mp.subplot(gs[0, :2])
mp.text(0.5, 0.5, 1, size=36,
        ha='center', va='center')
mp.subplot(gs[2, 1:])
mp.text(0.5, 0.5, 3, size=36,
        ha='center', va='center')
mp.subplot(gs[0:2, 2])
mp.text(0.5, 0.5, 2, size=36,
        ha='center', va='center')
mp.subplot(gs[1:, 0])
mp.text(0.5, 0.5, 4, size=36,
        ha='center', va='center')
mp.subplot(gs[1, 1])
mp.text(0.5, 0.5, 5, size=36,
        ha='center', va='center')
mp.show()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1026568.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号