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()