【漫漫科研路\pgfplots】子图的多种画法

2024-03-24 14:58

本文主要是介绍【漫漫科研路\pgfplots】子图的多种画法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在科研论文写作中,有时候为了横向、纵向对比或者节省空间,我们需要画子图,在MATLAB中可以通过subplot命令来实现。在Latex中有以下几种方法进行子图的绘制:
- 使用subfig宏包(有可能与hyperref宏包冲突,推荐使用subcaption宏包),主要格式为:

  \begin{figure}\subfloat[]{}\subfloat[]{}\\\subfloat[]{}\subfloat[]{}\end{figure}
  • 使用subcaption宏包,主要格式为:
  \begin{figure}\subcaptionbox{}{}\subcaptionbox{}{}\\\subcaptionbox{}{}\subcaptionbox{}{}\end{figure}
  • 使用groupplot宏包,主要格式为:
  \begin{figure}
\centering
\begin{tikzpicture}
\begin{groupplot}
\nextgroupplot
\addplot {x};
\nextgroupplot
\end{groupplot}
\end{tikzpicture}
\end{figure}
  • 使用matrix宏包,主要格式为:
    \begin{figure}
\centering
\begin{tikzpicture}
\matrix{\begin{axis}\addplot {x};\end{axis}\begin{axis}\addplot {x};\end{axis}}
\end{tikzpicture}
\end{figure}

下面给出上述各种情况的具体代码实现以及示意图:
- 使用subfig宏包
这里写图片描述
代码如下:

\documentclass{article}
\usepackage[dvipdfm]{graphicx}
\def\pgfsysdriver{pgfsys-dvipdfmx.def}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{subfig}%使用子图包,可能与hyperref冲突
\usepackage{float}
\usepackage{cite}
\usepackage[colorlinks,dvipdfm,pdfpagemode=UseOutlines,pdfstartview=FitH,anchorcolor=green,citecolor=blue,linkcolor=red,hyperindex=true,pagebackref,bookmarksnumbered,bookmarksopenlevel=2,colorlinks]{hyperref}\pgfplotsset{width=6cm,compat=1.15}\begin{document}
\begin{figure}
\begin{center}
\subfloat[\label{subfig1}]{
\begin{tikzpicture}
\begin{axis}[
legend columns=-1,%the legend are plotted horizontally
legend entries={$x$},
legend to name=named,% stored in named, not plotted in the figure
title={subfig1},
]
\addplot {x};\label{curvex}
\end{axis}
\end{tikzpicture}\label{subfig2}}
\subfloat[]{
\begin{tikzpicture}
\begin{axis}[title={subfig2}]
\addplot {x};
\end{axis}
\end{tikzpicture}\label{subfig2}}
\end{center}
\caption{\hspace{1em}Two subfigures.}\label{f1}
\end{figure}
As depicted in Figures~\ref{subfig1} and \ref{subfig2}, the subfigures of Figure~\ref{f1}, \ref{curvex} represents function $f(x)=x$.\end{document} 
  • 使用subcaption宏包
    这里写图片描述
    代码如下:
\documentclass{article}
\usepackage[dvipdfm]{graphicx}
\def\pgfsysdriver{pgfsys-dvipdfmx.def}
\usepackage{tikz}
\usepackage{pgfplots}
%\usepackage{subfig}
\usepackage[hypcap=true,labelsep=period,font=small]{caption}% 图的标题设置Fig.
\usepackage[hypcap=true]{subcaption}%用于画子图 可以适配hyperref包
\usepackage{float}
\usepackage[colorlinks,dvipdfm,pdfpagemode=UseOutlines,pdfstartview=FitH,anchorcolor=green,citecolor=blue,linkcolor=red,hyperindex=true,pagebackref,bookmarksnumbered,bookmarksopenlevel=2,colorlinks]{hyperref}
\pgfplotsset{width=6cm,compat=1.15}
\begin{document}
\begin{figure}
\begin{center}
\subcaptionbox{\label{subfig1}}{
\begin{tikzpicture}
\begin{axis}[
legend columns=-1,%the legend are plotted horizontally
legend entries={$x$},
legend to name=named,% stored in named
title={subfig1},
]
\addplot {x};\label{curvex}
\end{axis}
\end{tikzpicture}}
\subcaptionbox{\label{subfig2}}{
\begin{tikzpicture}
\begin{axis}[title={subfig2}]
\addplot {x};
\end{axis}
\end{tikzpicture}}
\end{center}
\caption{\hspace{1em}Two subfigures.}\label{f1}
\end{figure}As depicted in Figures~\ref{subfig1} and \ref{subfig2}, the subfigures of Figure~\ref{f1}, \ref{curvex} represents function $f(x)=x$.\end{document} 
  • 使用groupplot宏包
    这里写图片描述
    代码如下:
\documentclass{article}
\usepackage[dvipdfm]{graphicx}
\def\pgfsysdriver{pgfsys-dvipdfmx.def}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.15}
\usepgfplotslibrary{groupplots}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[colorlinks,dvipdfm,pdfpagemode=UseOutlines,pdfstartview=FitH,anchorcolor=green,citecolor=blue,linkcolor=red,hyperindex=true,pagebackref,bookmarksnumbered,bookmarksopenlevel=2,colorlinks]{hyperref}
\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}
\begin{groupplot}[
group style={
group name=my plots,
group size=2 by 2,
xlabels at=edge bottom,
xlabels at=all,
ylabels at=edge left,
x descriptions at=edge bottom,
},
footnotesize,
width=6cm,
height=6cm,
%
xlabel=$x$,
ylabel=$f(x)$,
]
\nextgroupplot
\addplot {x};
\node [text width=1em,anchor=north west] at (rel axis cs: 0,1){\subcaption{\label{f11}}};%<- changed
\nextgroupplot
\addplot {x^2};
\nextgroupplot
\addplot {x^3};
\nextgroupplot
\addplot {x^4};
\end{groupplot}
\end{tikzpicture}
\caption{\hspace{1em}Four subfigures.}\label{f1}
\end{figure}
How to refer to subfigure~\ref{f11} in Figure~\ref{f1}.\end{document} 
  • 使用matrix宏包
    这里写图片描述
    代码如下:
\documentclass{article}
\usepackage[dvipdfm]{graphicx}
\def\pgfsysdriver{pgfsys-dvipdfmx.def}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.15}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{matrix}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[colorlinks,dvipdfm,pdfpagemode=UseOutlines,pdfstartview=FitH,anchorcolor=green,citecolor=blue,linkcolor=red,hyperindex=true,pagebackref,bookmarksnumbered,bookmarksopenlevel=2,colorlinks]{hyperref}
\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}
\pgfplotsset{small}
\matrix {
\begin{axis}[ylabel={$f(x)=x$},ylabel style={font=\small}]
\addplot {x};
\node [text width=1em,anchor=north west] at (rel axis cs: 0,1){\subcaption{\label{f11}}};%<- changed
\end{axis}
&
% differently large labels are aligned automatically:
\begin{axis}[ylabel={$f(x)=x^2$},ylabel style={font=\small}]
\addplot {x^2};
\end{axis}
\\
\begin{axis}[ylabel={$f(x)=x^3$},ylabel style={font=\small},xlabel=$x$,xlabel style={font=\small}]
\addplot {x^3};
\end{axis}
&
\begin{axis}[ylabel={$f(x)=x^4$},ylabel style={font=\small},xlabel=$x$,xlabel style={font=\small}]
\addplot {x^4};
\end{axis}
\\
};
\end{tikzpicture}
\caption{\hspace{1em}Four subfigures.}\label{f1}
\end{figure}
How to refer to subfigure~\ref{f11} in Figure~\ref{f1}.\end{document} 

Note: 第三种和第四种方法不适合需要单独引用每一个子图的情况,比较适合把四张图看成一个整体的情况。


参考文献:
[1] pgfplots manual: http://pgfplots.sourceforge.net/pgfplots.pdf


本科舍友不幸得了黑色素癌, 在此恳求大家能给予帮助,在此谢谢大家!以下是水滴筹的链接:
https://www.shuidichou.com/cf/contribute/7d975cfc-e508-4549-acb1-5a31ceb759a1?channel=wx_charity_pyq&source=7hEPKZfbC7kzc7ndTKDD6cZriDdYH4TQ&forwardFrom=5&sharedv=2008
今天发现该链接已经提现了,但是已筹金额未达预期,期望各位通过左下角的微信扫码进行捐款,我将悉数给予舍友。再次感谢大家的帮助!

这篇关于【漫漫科研路\pgfplots】子图的多种画法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/841944

相关文章

查询SQL Server数据库服务器IP地址的多种有效方法

《查询SQLServer数据库服务器IP地址的多种有效方法》作为数据库管理员或开发人员,了解如何查询SQLServer数据库服务器的IP地址是一项重要技能,本文将介绍几种简单而有效的方法,帮助你轻松... 目录使用T-SQL查询方法1:使用系统函数方法2:使用系统视图使用SQL Server Configu

Redis多种内存淘汰策略及配置技巧分享

《Redis多种内存淘汰策略及配置技巧分享》本文介绍了Redis内存满时的淘汰机制,包括内存淘汰机制的概念,Redis提供的8种淘汰策略(如noeviction、volatile-lru等)及其适用场... 目录前言一、什么是 Redis 的内存淘汰机制?二、Redis 内存淘汰策略1. pythonnoe

SpringBoot项目启动后自动加载系统配置的多种实现方式

《SpringBoot项目启动后自动加载系统配置的多种实现方式》:本文主要介绍SpringBoot项目启动后自动加载系统配置的多种实现方式,并通过代码示例讲解的非常详细,对大家的学习或工作有一定的... 目录1. 使用 CommandLineRunner实现方式:2. 使用 ApplicationRunne

Python中实现进度条的多种方法总结

《Python中实现进度条的多种方法总结》在Python编程中,进度条是一个非常有用的功能,它能让用户直观地了解任务的进度,提升用户体验,本文将介绍几种在Python中实现进度条的常用方法,并通过代码... 目录一、简单的打印方式二、使用tqdm库三、使用alive-progress库四、使用progres

Java文件上传的多种实现方式

《Java文件上传的多种实现方式》文章主要介绍了文件上传接收接口的使用方法,包括获取文件信息、创建文件夹、保存文件到本地的两种方法,以及如何使用Postman进行接口调用... 目录Java文件上传的多方式1.文件上传接收文件接口2.接口主要内容部分3.postman接口调用总结Java文件上传的多方式1

C#中字符串分割的多种方式

《C#中字符串分割的多种方式》在C#编程语言中,字符串处理是日常开发中不可或缺的一部分,字符串分割是处理文本数据时常用的操作,它允许我们将一个长字符串分解成多个子字符串,本文给大家介绍了C#中字符串分... 目录1. 使用 string.Split2. 使用正则表达式 (Regex.Split)3. 使用

如何测试计算机的内存是否存在问题? 判断电脑内存故障的多种方法

《如何测试计算机的内存是否存在问题?判断电脑内存故障的多种方法》内存是电脑中非常重要的组件之一,如果内存出现故障,可能会导致电脑出现各种问题,如蓝屏、死机、程序崩溃等,如何判断内存是否出现故障呢?下... 如果你的电脑是崩溃、冻结还是不稳定,那么它的内存可能有问题。要进行检查,你可以使用Windows 11

科研绘图系列:R语言扩展物种堆积图(Extended Stacked Barplot)

介绍 R语言的扩展物种堆积图是一种数据可视化工具,它不仅展示了物种的堆积结果,还整合了不同样本分组之间的差异性分析结果。这种图形表示方法能够直观地比较不同物种在各个分组中的显著性差异,为研究者提供了一种有效的数据解读方式。 加载R包 knitr::opts_chunk$set(warning = F, message = F)library(tidyverse)library(phyl

科研小白成长记40——第三个五年计划

小gap期间,拼命玩和拼命休息的同时,仔细思考了下我期望的五年之后的样子,gap结束,算是目标愈发清晰起来。曾经,读博的目标是成为一名independent researcher,并且具备发至少一篇顶会的能力。而现在,希望五年后的自己,成为一名good independent researcher。当然,这里的good,根据现阶段的科研榜样,已经有了具体的metrics。 首先是随时在线的深度理解

用imagebox控件实现多种视频播放功能,并且帧图片可供后续处理

深夜还在打代码(而且不止一天)的我,必须来报复一下自己的智商,不然许多的怒气和无奈都难以独自承受。         作为非计算机专业的我,对代码有着极其无法言说的热情,但是这件事是我大二才发现的,ps:大学之前几乎没玩过电脑(家长管得不是一般的严,玩电脑跟抢银行同罪,所以自认孝顺的我是不会踏入禁区的)。        大学期间为了做自己喜欢的事,放弃了很多,也不顾别人的眼光。起