在科研論文寫作中,我們經常需要放大局部圖片來顯示細節,即繪製圖中圖。在Matlab中可以使用magnify或則axes函數,網上有很多例子,這裏不再贅述。本文主要講解如何使用tikz/pgfplots來畫局部放大圖中圖。
繪製局部放大圖,需要使用到spy宏包,本文主要參考pgfplots手冊。下面給出一個最簡單的實例,具體代碼如下:
\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}
\usetikzlibrary{spy} %
\pgfplotsset{width=7cm,compat=1.14}
\begin{document}
\begin{tikzpicture}[spy using outlines= {circle, magnification=6, connect spies}]
% define the shape of spy: circle or rectangle
\begin{axis}[no markers,grid=major, every axis plot post/.append style={thick}]
\addplot coordinates {(0, 0) (0, 0.9) (1, 0.9) (2, 1) (3, 0.9) (80, 0)};
\addplot+ [line join=round] coordinates {(0, 0) (0, 0.9) (2, 0.9) (3, 1) (4, 0.9) (80, 0)};
\addplot+ [line join=bevel] coordinates {(0, 0) (0, 0.9) (3, 0.9) (4, 1) (5, 0.9) (80, 0)};
\addplot+ [miter limit=5] coordinates {(0, 0) (0, 0.9) (4, 0.9) (5, 1) (6, 0.9) (80, 0)};
\coordinate (spypoint) at (3,1);% The point to be magnified
\coordinate (magnifyglass) at (60,0.7);% The point where to see
\end{axis}
\spy [blue, size=2.5cm] on (spypoint) in node[fill=white] at (magnifyglass);
\end{tikzpicture}
\end{document}
結果如下:
代碼簡單易懂,也給出了部分註釋。爲了使得更加個性化,我們可以修改放大圖的輪廓,或則背景色,具體代碼如下:
\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}
\usetikzlibrary{spy} %
\pgfplotsset{width=7cm,compat=1.14}
\begin{document}
\begin{tikzpicture}[spy using overlays= {rectangle, magnification=6, connect spies}]
% define the shape of spy: circle or rectangle
\begin{axis}[no markers,grid=major, every axis plot post/.append style={thick}]
\addplot coordinates {(0, 0) (0, 0.9) (1, 0.9) (2, 1) (3, 0.9) (80, 0)};
\addplot+ [line join=round] coordinates {(0, 0) (0, 0.9) (2, 0.9) (3, 1) (4, 0.9) (80, 0)};
\addplot+ [line join=bevel] coordinates {(0, 0) (0, 0.9) (3, 0.9) (4, 1) (5, 0.9) (80, 0)};
\addplot+ [miter limit=5] coordinates {(0, 0) (0, 0.9) (4, 0.9) (5, 1) (6, 0.9) (80, 0)};
\coordinate (spypoint) at (3,1);% The point to be magnified
\coordinate (magnifyglass) at (60,0.7);% The point where to see
\end{axis}
\spy [green,size=2.5cm] on (spypoint) in node at (magnifyglass);
\end{tikzpicture}
\end{document}
顯示效果如下:
由上圖可以看到,相比於Matlab,這裏局部放大圖更加靈活和個性化。但是Matlab的局部放大圖一般都有座標軸,方便查看局部圖的值大小。在Tikz/pgfplots中利用Spy宏包實現相同的效果比較困難。但是我們可以另闢蹊徑: 只需要利用node,在node中重新畫一個圖,只是自己設置橫縱座標的值,從而實現局部放大。具體代碼實現如下:
\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}
\usetikzlibrary{spy} %
\pgfplotsset{width=7cm,compat=1.14}
\begin{document}
\begin{tikzpicture}[pin distance=1.5cm]
\begin{axis}[no markers,grid=major, every axis plot post/.append style={thick}]
\addplot coordinates {(0, 0) (0, 0.9) (1, 0.9) (2, 1) (3, 0.9) (80, 0)};
\addplot+ [line join=round] coordinates {(0, 0) (0, 0.9) (2, 0.9) (3, 1) (4, 0.9) (80, 0)};
\addplot+ [line join=bevel] coordinates {(0, 0) (0, 0.9) (3, 0.9) (4, 1) (5, 0.9) (80, 0)};
\addplot+ [miter limit=5] coordinates {(0, 0) (0, 0.9) (4, 0.9) (5, 1) (6, 0.9) (80, 0)};
\coordinate (spy_point) at (axis cs:0,1);
\end{axis}
%plot the magnified figure using a node
\node[pin=-5:{%
\begin{tikzpicture}
\begin{axis}[no markers, thick,scale=0.8,
tiny,
xlabel={x},
ylabel={y},
xmin=0,xmax=8,
ymin=0.85,ymax=1.05,
enlargelimits,
]
\addplot coordinates {(0, 0) (0, 0.9) (1, 0.9) (2, 1) (3, 0.9) (80, 0)};
\addplot+ [line join=round] coordinates {(0, 0) (0, 0.9) (2, 0.9) (3, 1) (4, 0.9) (80, 0)};
\addplot+ [line join=bevel] coordinates {(0, 0) (0, 0.9) (3, 0.9) (4, 1) (5, 0.9) (80, 0)};
\addplot+ [miter limit=5] coordinates {(0, 0) (0, 0.9) (4, 0.9) (5, 1) (6, 0.9) (80, 0)};
\end{axis}
\end{tikzpicture}%
}] at (spy_point) {};
\end{tikzpicture}
結果如下: