【漫漫科研路\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

相关文章

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Java 中实现异步的多种方式

《Java中实现异步的多种方式》文章介绍了Java中实现异步处理的几种常见方式,每种方式都有其特点和适用场景,通过选择合适的异步处理方式,可以提高程序的性能和可维护性,感兴趣的朋友一起看看吧... 目录1. 线程池(ExecutorService)2. CompletableFuture3. ForkJoi

mss32.dll文件丢失怎么办? 电脑提示mss32.dll丢失的多种修复方法

《mss32.dll文件丢失怎么办?电脑提示mss32.dll丢失的多种修复方法》最近,很多电脑用户可能遇到了mss32.dll文件丢失的问题,导致一些应用程序无法正常启动,那么,如何修复这个问题呢... 在电脑常年累月的使用过程中,偶尔会遇到一些问题令人头疼。像是某个程序尝试运行时,系统突然弹出一个错误提

C++字符串提取和分割的多种方法

《C++字符串提取和分割的多种方法》在C++编程中,字符串处理是一个常见的任务,尤其是在需要从字符串中提取特定数据时,本文将详细探讨如何使用C++标准库中的工具来提取和分割字符串,并分析不同方法的适用... 目录1. 字符串提取的基本方法1.1 使用 std::istringstream 和 >> 操作符示

python展开嵌套列表的多种方法

《python展开嵌套列表的多种方法》本文主要介绍了python展开嵌套列表的多种方法,包括for循环、列表推导式和sum函数三种方法,具有一定的参考价值,感兴趣的可以了解一下... 目录一、嵌套列表格式二、嵌套列表展开方法(一)for循环(1)for循环+append()(2)for循环+pyPhWiFd

Python实现PDF与多种图片格式之间互转(PNG, JPG, BMP, EMF, SVG)

《Python实现PDF与多种图片格式之间互转(PNG,JPG,BMP,EMF,SVG)》PDF和图片是我们日常生活和工作中常用的文件格式,有时候,我们可能需要将PDF和图片进行格式互转来满足... 目录一、介绍二、安装python库三、Python实现多种图片格式转PDF1、单张图片转换为PDF2、多张图

电脑开机提示krpt.dll丢失怎么解决? krpt.dll文件缺失的多种解决办法

《电脑开机提示krpt.dll丢失怎么解决?krpt.dll文件缺失的多种解决办法》krpt.dll是Windows操作系统中的一个动态链接库文件,它对于系统的正常运行起着重要的作用,本文将详细介绍... 在使用 Windows 操作系统的过程中,用户有时会遇到各种错误提示,其中“找不到 krpt.dll”

python多种数据类型输出为Excel文件

《python多种数据类型输出为Excel文件》本文主要介绍了将Python中的列表、元组、字典和集合等数据类型输出到Excel文件中,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一.列表List二.字典dict三.集合set四.元组tuplepython中的列表、元组、字典

电脑报错cxcore100.dll丢失怎么办? 多种免费修复缺失的cxcore100.dll文件的技巧

《电脑报错cxcore100.dll丢失怎么办?多种免费修复缺失的cxcore100.dll文件的技巧》你是否也遇到过“由于找不到cxcore100.dll,无法继续执行代码,重新安装程序可能会解... 当电脑报错“cxcore100.dll未找到”时,这通常意味着系统无法找到或加载这编程个必要的动态链接库

MyBatis-Plus中静态工具Db的多种用法及实例分析

《MyBatis-Plus中静态工具Db的多种用法及实例分析》本文将详细讲解MyBatis-Plus中静态工具Db的各种用法,并结合具体案例进行演示和说明,具有很好的参考价值,希望对大家有所帮助,如有... 目录MyBATis-Plus中静态工具Db的多种用法及实例案例背景使用静态工具Db进行数据库操作插入