【漫漫科研路\pgfplots】画局部放大图
生活随笔
收集整理的這篇文章主要介紹了
【漫漫科研路\pgfplots】画局部放大图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在科研論文寫作中,我們經常需要放大局部圖片來顯示細節,即繪制圖中圖。在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}結果如下:
總結
以上是生活随笔為你收集整理的【漫漫科研路\pgfplots】画局部放大图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安徽省计算机二级VB笔试题,[考试中心]
- 下一篇: 天气预报模块