分析测试百科网

搜索

喜欢作者

微信支付微信支付
×

解析Python中利用Matplotlib绘制多图并合并展示(一)

2020.9.29
头像

王辉

致力于为分析测试行业奉献终身

大家好,最近在研究在搞Python的大作业,有个需求就是利用Matplotlib画几个像模像样的统计图然后合并在一张图中,因为此前很少用这方面的东西,所以折腾了不少时间,今天介绍一下。

wx_article_20200427140844_qLZpdv.jpg

1、subplot多合一

其实,利用python 的matplotlib包下的subplot函数可以将多个子图放在同一个画板上。在此之前,我们先来看一个案例:

import  matplotlib.pyplot as plt

import numpy as np 

plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签

plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号

t=np.arange(0.0,2.0,0.1)

s=np.sin(t*np.pi)

plt.figure(figsize=(8,8), dpi=80)

plt.figure(1)

ax1 = plt.subplot(221)

ax1.plot(t,s, color="r",linestyle = "--")

ax2 = plt.subplot(222)

ax2.plot(t,s,color="y",linestyle = "-")

ax3 = plt.subplot(223)

ax3.plot(t,s,color="g",linestyle = "-.")

ax4 = plt.subplot(224)

ax4.plot(t,s,color="b",linestyle = ":")

效果如下:

wx_article_20200427140844_3zu8Wi.jpg

可以看到,一个画板上放了4个子图。达到了我们想要的效果。好了我们现在来解析一下刚刚的部分代码:


互联网
仪器推荐
文章推荐