Matplotlib 多图显示 重叠显示

Matplotlib-多图显示

系列目录:




提示:以下是本篇文章正文内容,下面案例可供参考

一、.add_subplot()

1.参数解读

nrows行数
ncols列数
index位置索引

add_subplot(233) 两行 三列 第三个
在这里插入图片描述

2、案例

代码如下(示例):

import matplotlib.pyplot as plt
#准备数据
x1 = ['A', 'B', 'C', 'D', 'E']
y1 = [23,17,35,29,12]
#创建图形对象
fig = plt.figure()
#添加子图区域,参数值表示[left, bottom, width, height ]


# plt.subplot()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)

#绘制柱状图
ax1.bar(x1,y1)
plt.show()

在这里插入图片描述

二、.add_axes([ left, bottom, width, height ])

1. 参数解读

left bottom代表 距离左下角距离 数值在0~1 之间 百分比作距离
width height 代表子图比例 数值在在0~1 之间 百分比作长度

2.并列显示案例

代码如下(示例):

import matplotlib.pyplot as plt
#创建图形对象
fig = plt.figure()
#添加子图区域,参数值表示[left, bottom, width, height ]
#left bottom代表 距离左下角距离  width height 代表比例 在0~1 之间
ax1 = fig.add_axes([0,0.5,0.4,0.4])
ax2 = fig.add_axes([0.5,0.5,0.4,0.4])
ax3 = fig.add_axes([0,0,0.4,0.4])
ax4 = fig.add_axes([0.5,0,0.4,0.4])
#准备数据
x1 = ['A', 'B', 'C', 'D', 'E']
y1 = [23,17,35,29,12]
#绘制柱状图
ax1.bar(x1,y1)
plt.show()

在这里插入图片描述

3.重叠显示案例

代码如下(示例):

import matplotlib.pyplot as plt
#创建图形对象
fig = plt.figure()
#添加子图区域,参数值表示[left, bottom, width, height ]
#left bottom代表 距离左下角距离  width height 代表比例 在0~1 之间
ax1 = fig.add_axes([0,0,1,1])
ax2 = fig.add_axes([0.6,0.6,0.4,0.4])
#准备数据
x1 = ['A', 'B', 'C', 'D', 'E']
y1 = [23,17,35,29,12]

x2 = ['A', 'B', 'C', 'D', 'E']
y2 = [23,17,35,29,12]
#绘制柱状图
ax1.bar(x1,y1)
#绘制折线图
ax2.plot(x2,y2)
plt.show()

在这里插入图片描述


  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值