引言

前面一篇文章 Python 绘制直方图 Matplotlib Pyplot figure bar legend gca text 有介绍如何用 Python 绘制直方图,但现实应用中需要在直方图的基础上再绘制折线图。比如测试报告中,就可以用图形反映测试用例的运行情况,直方图可以反映测试用例 pass 了多少,失败了多少,如果想反映 pass rate 趋势,就可以用折线图,本文介绍如何绘制多个图,以及最后应用在同一图上绘制直方图和折线图并存。

内容提要:

  1. subplot 和 subplots 函数
  2. twinx 函数
  3. 直方图和折线图并存例子

subplot 和 subplots 函数

plt.figure 的作用是定义一个大的图纸,可以设置图纸的大小、分辨率等。
例如:
初始化一张画布
fig = plt.figure(figsize=(7,10),dpi=100)

直接在当前活跃的的 axes 上面作图,注意是当前活跃的
plt.plot()plt.bar()

那么现在来看 subplotsubplots ,两者的区别在于 suplots 绘制多少图已经指定了,所以 ax 提前已经准备好了,而 subplot 函数调用一次就绘制一次,没有指定。

subplot 函数

subplot 函数是添加一个指定位置的子图到当前画板中。

matplotlib.pyplot.subplot(*args, **kwargs)

函数签名:

subplot(nrows, ncols, index, **kwargs) 括号里的数值依次表示行数、列数、第几个
subplot(pos, **kwargs)
subplot(ax) 这个我没用应用成功

如果需要自定义画板大小,使用 subplot 这个函数时需要先定义一个自定义大小的画板,因为 subplot 函数无法更改画板的大小和分辨率等信息;所以必须通过 fig = plt.figure(figsize=(12, 4), dpi=200) 来定义画板相关设置;不然就按默认画板的大小; 同时,后续对于这个函数便捷的操作就是直接用 plt,获取当前活跃的图层。

例如:添加 4 个子图
没有自定义画板,所以是在默认大小的画板上添加子图的。
plt.subplot(221) 中的 221 表示子图区域划分为 2 行 2 列,取其第一个子图区域。子区编号从左上角为 1 开始,序号依次向右递增。

import matplotlib.pyplot as plt# equivalent but more general than plt.subplot(221)ax1 = plt.subplot(2, 2, 1)# add a subplot with no frameax2 = plt.subplot(222, frameon=False)# add a polar subplotplt.subplot(223, projection='polar')# add a red subplot that shares the x-axis with ax1plt.subplot(224, sharex=ax1, facecolor='red')plt.show()

效果:

subplots 函数

subplots 函数主要是创建一个画板和一系列子图。

matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)

该函数返回两个变量,一个是 Figure 实例,另一个 AxesSubplot 实例 。Figure 代表整个图像也就是画板整个容器,AxesSubplot 代表坐标轴和画的子图,通过下标获取需要的子区域。

例 1:一个画板中只有 1 个子图
plt.subplots() 默认 1 行 1 列

 import numpy as npimport matplotlib.pyplot as plt# First create some toy data:x = np.linspace(0, 2*np.pi, 400)y = np.sin(x**2)# Creates just a figure and only one subplotfig, ax = plt.subplots()ax.plot(x, y)ax.set_title('Simple plot')plt.show()


例 2:一个画板中只有 2 个子图
plt.subplots(1, 2)1 行 2 列

 import numpy as npimport matplotlib.pyplot as plt# First create some toy data:x = np.linspace(0, 2*np.pi, 400)y = np.sin(x**2)# Creates two subplots and unpacks the output array immediatelyf, (ax1, ax2) = plt.subplots(1, 2, sharey=True)ax1.plot(x, y)ax1.set_title('Sharing Y axis')ax2.scatter(x, y)plt.show()


例 3:一个画板中只有 4 个子图
plt.subplots(2, 2)2 行 2 列
通过索引来访问子图:
axes[0, 0] 第一个图位置
axes[0, 1] 第二个图位置
axes[1, 0] 第三个图位置
axes[1, 1] 第四个图位置

 import numpy as npimport matplotlib.pyplot as plt# First create some toy data:x = np.linspace(0, 2*np.pi, 400)y = np.sin(x**2)# Creates four polar axes, and accesses them through the returned arrayfig, axes = plt.subplots(2, 2, subplot_kw=dict(polar=True))axes[0, 0].plot(x, y)axes[1, 1].scatter(x, y)plt.show()


例 4:子图共享 X 轴

sharex=‘col’

     import numpy as npimport matplotlib.pyplot as plt# Share a X axis with each column of subplotsplt.subplots(2, 2, sharex='col')plt.show()


例 5:子图共享 Y 轴

sharey=‘row’

     import numpy as npimport matplotlib.pyplot as plt# Share a X axis with each column of subplotsplt.subplots(2, 2, sharey='row')plt.show()


例 6:子图共享 X 和 Y 轴
sharex=‘all’, sharey=‘all’

     import numpy as npimport matplotlib.pyplot as plt# Share a X axis with each column of subplotsplt.subplots(2, 2, sharex='all', sharey='all')# Note that this is the same as# plt.subplots(2, 2, sharex=True, sharey=True)plt.show()


例 7:创建制定编号的画图
编号为 10 的画板,如果该编号已经存在,则删除编号。

     import numpy as npimport matplotlib.pyplot as plt# Creates figure number 10 with a single subplot# and clears it if it already exists.fig, ax=plt.subplots(num=10, clear=True)plt.show()

twinx 函数

了解了 subplots 函数,对 twinx 函数的理解就容易多了。

twinx 返回一个新的子图并共用当前子图的 X 轴。新的子图会覆盖 X 轴(如果新子图 X 轴是 None 就会用当前子图的 X 轴数据),其 Y 轴会在右侧。同理 twiny 函数是共享 Y 轴的。

例:我们先画了一个红色线子图,再画一个蓝色线子图,蓝色线子图是共用红色线子图的 X 轴的,那么就可以实样实现:

fig, ax1 = plt.subplots() 生成一个画板 fig,并默认第一个子图 ax1.
ax1.plot(t, data1, color=color) ax1 第一子图区域画折线图
ax2 = ax1.twinx() 生成一个新的子图 ax2 ,并共享 ax1 第一子图的 X 轴
ax2.plot(t, data2, color=color) 在新的子图 ax2 区域画折线图

效果图:

代码:

 import numpy as npimport matplotlib.pyplot as plt# Create some mock datat = np.arange(0.01, 10.0, 0.01)data1 = np.exp(t)data2 = np.sin(2 * np.pi * t)fig, ax1 = plt.subplots()color = 'tab:red'ax1.set_xlabel('time (s)')ax1.set_ylabel('exp', color=color)ax1.plot(t, data1, color=color)ax1.tick_params(axis='y', labelcolor=color)ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axiscolor = 'tab:blue'ax2.set_ylabel('sin', color=color)  # we already handled the x-label with ax1ax2.plot(t, data2, color=color)ax2.tick_params(axis='y', labelcolor=color)fig.tight_layout()  # otherwise the right y-label is slightly clippedplt.show()

应用:直方图和折线图并存

有了前面的基础,我们来画一个直方图和折线图并存。基于前面一篇文章 Python 绘制直方图 Matplotlib Pyplot figure bar legend gca text 直方图的例子基础上再画折线图。

应用场景是测试用例运行结果图,直方图描述 Smoke 和 Regressin 用例pass 和 fail 数量,折线图描述总的用例 Pass Rate。把 Mock 的数字换一下就可以直接应用了,具体代码细节就不详细介绍了,可以参考 文章 Python 绘制直方图 Matplotlib Pyplot figure bar legend gca text 。

效果图:

代码:

import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
import numpy as np
from matplotlib.ticker import FuncFormatterdef to_percent(num, position):return str(num) + '%'def draw_trend_image_test(reg_total_count_pass, reg_total_count_fail, smoke_total_count_pass, smoke_total_count_fail, date_list, image_file_name = 'test_result_trend.png'):# attributesbar_width = 0.3regression_pass_color = '#0ac00a'smoke_pass_color = '#068606'font_name = 'Calibri'label_size = 10text_size = 8title_size = 14# set the color for failuresmoke_fail_color = []for item in smoke_total_count_fail:if item > 0:smoke_fail_color.append("red")else:smoke_fail_color.append(smoke_pass_color)   reg_fail_color = []for item in reg_total_count_fail:if item > 0:reg_fail_color.append("red")else:reg_fail_color.append(regression_pass_color)   total_pass_rate = [round((s_p+r_p)/(s_p+r_p+s_f+r_f), 2)*100 for s_p, r_p, s_f, r_f in zip(smoke_total_count_pass, reg_total_count_pass, smoke_total_count_fail, reg_total_count_fail)]       if len(date_list) > 5:fig, ax1 = plt.subplots(figsize=(10.8, 4.8))else:fig, ax1 = plt.subplots()# draw barx = np.arange(len(date_list))ax1.bar(x - bar_width/2, smoke_total_count_pass, color=smoke_pass_color, edgecolor=smoke_pass_color, width= bar_width, label="Smoke Passed")ax1.bar(x - bar_width/2, smoke_total_count_fail, color="red", edgecolor=smoke_fail_color, width= bar_width, bottom=smoke_total_count_pass)ax1.bar(x + bar_width/2, reg_total_count_pass, color=regression_pass_color, edgecolor=regression_pass_color, width= bar_width, label="Regression Passed")ax1.bar(x + bar_width/2, reg_total_count_fail, color="red", edgecolor=reg_fail_color, width= bar_width, label="Failed", bottom=reg_total_count_pass)# set title, labelsax1.set_title("Test Result Trend", fontsize=title_size, fontname=font_name)ax1.set_xticks(x, date_list,fontsize=label_size, fontname=font_name)ax1.set_ylabel("Count",fontsize=label_size, fontname=font_name)# set bar textfor i in x:if smoke_total_count_fail[i] > 0:ax1.text(i-bar_width/2, smoke_total_count_fail[i] + smoke_total_count_pass[i], smoke_total_count_fail[i],horizontalalignment = 'center', verticalalignment='bottom',fontsize=text_size,family=font_name,color='red',weight='bold')   ax1.text(i-bar_width, smoke_total_count_pass[i], smoke_total_count_pass[i],horizontalalignment = 'right', verticalalignment='top',fontsize=text_size,family=font_name,color=smoke_pass_color,weight='bold')ax1.text(i, reg_total_count_pass[i], reg_total_count_pass[i], horizontalalignment = 'right', verticalalignment='top',fontsize=text_size,family=font_name,color=regression_pass_color,weight='bold')if reg_total_count_fail[i] > 0:ax1.text(i+ bar_width/2, reg_total_count_fail[i] + reg_total_count_pass[i], reg_total_count_fail[i],horizontalalignment = 'center', verticalalignment='bottom',fontsize=text_size,family=font_name,color='red',weight='bold')   # draw plotax2 = ax1.twinx()ax2.plot(x, total_pass_rate, label='Total Pass Rate',linewidth=2, color='#FFB90F')ax2.yaxis.set_major_formatter(FuncFormatter(to_percent))ax2.set_ylim(0, 100)ax2.set_ylabel("Percent",fontsize=label_size, fontname=font_name)# plt.show() # should comment it if save the pic as a file, or the saved pic is blank# dpi: image resolution, better resolution, bigger image sizelegend_font = font_manager.FontProperties(family=font_name, weight='normal',style='normal', size=label_size)    fig.legend(loc="lower center", ncol=4, frameon=False, prop=legend_font)fig.savefig(image_file_name,dpi = 100)if  __name__ == '__main__':reg_total_count_pass = [916,916,916,906,916,716,916,916,916,916]reg_total_count_fail = [73,73,73,83,73,273,73,73,73,73]smoke_total_count_pass = [420, 420, 420,420, 420, 400,420, 420, 420,420] smoke_total_count_fail = [5,5,5,5,5,25,5,5,5,5]date_list = ['2022/1/1', '2022/1/2', '2022/1/3','2022/1/4','2022/1/5', '2022/1/6', '2022/1/7','2022/1/8', '2022/1/9', '2022/1/10']draw_trend_image_test(reg_total_count_pass, reg_total_count_fail, smoke_total_count_pass, smoke_total_count_fail, date_list)

Python - Matplot 绘制多图 直方图和折线图并存 共用 X 轴相关推荐

  1. python数据可视化(matplotlib条形图、饼图、箱状图、直方图、折线图)(代码)

    python数据可视化(matplotlib条形图.饼图.箱状图.直方图.折线图) matplotlib(条形图) 一.简单条形图 1.简单垂直条形图 2.简单水平条形图 二.水平交错条形图 三.垂直 ...

  2. python画两条曲线图_python绘制多个曲线的折线图

    这篇文章利用的是matplotlib.pyplot.plot的工具来绘制折线图,这里先给出一个段代码和结果图: # -*- coding: UTF-8 -*- import numpy as np i ...

  3. python实战二:使用CSV数据绘制带数据标志的折线图(matplotlib)

    背景: 自动获取缺陷管理系统中的bug趋势统计数据,并保存到CSV中,读取CSV数据并绘制带数据标志的折线图,并保存为png图片 下面代码仅实现"读取CSV数据并绘制带数据标志的折线图,并保 ...

  4. python绘制对比分析图(柱状图、折线图)

    所谓对比分析就是两个相互联系的指标进行比较 下面用例子说明,首先导入库,别名 因为我用的是jupyter notebook,后面需要用matplotlib画图,所以要加上%matplotlib inl ...

  5. 100天精通Python(可视化篇)——第79天:matplotlib绘制不同种类炫酷折线图代码实战(网格、趋势、对比、百分比、多条折线、堆积、百分比堆积、多坐标子图、3D折线图)

    文章目录 0. 专栏导读 1. 普通折线图 2. 网格折线图 3. 趋势折线图 4. 对比折线图 5. 百分比折线图 6. 多条折线图 7. 多坐标子图 8. 堆积折线图

  6. Python读取excel/csv表格并通过折线图可视化显示

    Python读取excel/csv表格并通过折线图可视化显示 写作背景 参数 使用figure()函数重新申请一个figure对象 注意,每次调用figure的时候都会重新申请一个figure对象 第 ...

  7. 实战py绘制物流行业快递业务量折线图

    大家好,我是执念斩长河.最近在学习python绘图.遇到一个"2018年.2019年物流行业的快递业务量"画图问题.在刚开始接触此题时,有点心慌意乱,上网查阅资料发现,题目原来这么 ...

  8. python常用画图(分段折线图、多变量柱状图、扇形图、堆积折线图、百分比堆积柱状图)工具代码汇总

    ​​​​​​在工作中常使用python绘制各类图形,之前通过CSDN学习到了很多,现在在这里对各类绘图工具及用法做一个总结,我将附上代码以及图片效果,以方便大家使用python进行图片绘制.需要注意一 ...

  9. Python使用matplotlib函数subplot可视化多个不同颜色的折线图、在折线图上为每个数据点添加日期数据标签

    Python使用matplotlib函数subplot可视化多个不同颜色的折线图.在折线图上为每个数据点添加日期数据标签 目录

  10. Python使用matplotlib函数subplot可视化多个不同颜色的折线图、在折线图上为每个数据点添加数值标签

    Python使用matplotlib函数subplot可视化多个不同颜色的折线图.在折线图上为每个数据点添加数值标签 目录

最新文章

  1. SQL Server数据库新建拥有部分查看操作权限的用户
  2. 【机器学习入门到精通系列】推荐系统之协同过滤算法
  3. bzoj 2962 序列操作
  4. 计算机专业毕业论文的参考文献,计算机专业毕业专著类参考文献 计算机专业毕业论文参考文献哪里找...
  5. 我所认识的SAP系统
  6. python模拟sed在每行添加##
  7. python实现文件上传和下载_[Python] socket实现TFTP上传和下载
  8. C和指针之实现strlen函数
  9. Apache JMeter 启动
  10. 两个苹果手机怎么传通讯录_苹果手机怎么导入通讯录?教你换机快速导入
  11. mysql路径猜解_猜解数据库(MYSQL)信息
  12. Codevs 3342 绿色通道
  13. js 获取浏览器高度、浏览器宽度
  14. CSS3 background 与 渐变
  15. 推荐系统9---AFM与DIN模型(推荐系统遇上注意力机制)
  16. 【音视频】获取桌面程序窗口列表以及桌面、窗口的缩略图(4-4)
  17. XILINX GTX学习笔记
  18. 如何打造一款游戏外挂
  19. AD597 K型热电偶放大器 温度测量模块 热电偶变送器 模拟输出 原理图PCB
  20. KBPC5010-ASEMI大功率整流桥、50A整流桥

热门文章

  1. js中英文字符与中文字符长度区别
  2. 关于大麦网接口抢票构造的一些思路
  3. 关联规则挖掘与Apriori算法
  4. Docker-基本命令和漏洞分享
  5. MSP430f149红外接收头——读任意红外遥控器键值并显示于数码管
  6. nginx 解析二级域名
  7. matlab 定义结构体数组,结构体数组及其定义和使用,C语言结构体数组详解
  8. book回车键 mac_macbook pro键盘失灵 macbook pro键盘失灵解决办法
  9. 多个jdk配置环境变量
  10. JDK 安装教程——图文细讲