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

效果圖如下:
在這裏插入圖片描述

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