【漫漫科研路\pgfplots】画双Y座标图

在科研论文写作中,经常会遇到画描述tradeoff的仿真图。比如在5G相关的研究中,经常会出现能效与时延的tradeoff。本文主要介绍如何在论文仿真部分,根据仿真结果(存储于.dat文件中)绘制双Y座标的曲线。


假定我们有两组仿真数据[x, y1] [x, y2],分布存储在y1.dat和y2.dat文件中。文件内容如下图所示:

在这里插入图片描述
在这里插入图片描述

画双Y座标,主要用到了\pgfplotsset{set layers}命令。在此命令的基础上,分布在两个座标系画图,就得到了想要的效果。相比于Matlab作图工具,这里的优势在于,可以用颜色区分两个Y轴。并且,利用pgfplots作图,图中曲线也可以被索引的优势,可以直接在y轴指明曲线类型来区分。

具体源代码如下:

\documentclass[10pt, final, journal, twocolumn, oneside]{IEEEtran}

%!TEX program = xelatex
% !TEX encoding = UTF-8  (utf8)
%!TEX spellcheck
%\def\pgfsysdriver{pgfsys-dvipdfmx.def}
\usepackage{tikz}

\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.14}\begin{document}
\begin{tikzpicture}
	\pgfplotsset{set layers}
\begin{axis}[scale only axis,
            grid=major,
			axis y line*=left,
             y axis line style={blue},
            y tick label style={blue},
             xlabel=x,
			 ylabel=\ref{y1}\color{blue}y1,]
			 \addplot [blue,mark=square] table[x index=0, y index=1,red] {y1.dat};\label{y1}
\end{axis}

\begin{axis}[scale only axis,
             grid=major,
             axis y line*=right,
             axis x line=none,
             y axis line style={red},
             y tick label style={red},
             ylabel=\ref{y2}\color{red}y2,
             ]
             \addplot [red,mark=asterisk] table[x index=0, y index=1] {y2.dat};\label{y2}
\end{axis}

\end{tikzpicture}

\end{document

效果图如下:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章