我正在尝试使用 pgfplots 绘制下图:
该图是用 matlab 生成的。
我可以用 pgfplots 轻松绘制两条曲线,但我不知道如何添加带有 0.3dB 标记的垂直线。也许有可能提取 y=10^-1 处两个图的 x(SNR)的(插值)值。应该提到的是,只有 20、22、24、26、28、30 和 32 dB 的 y 值可用。
这是我当前的 TeX 代码(绘图数据与 matlab 图片不完全吻合,但想法是一样的):
\documentclass[a4paper, ngerman]{scrreprt}
\usepackage{color}
% predefined colors
\definecolor{color1}{rgb}{0.44706,0.44706,0.44706}%
\definecolor{color2}{rgb}{0.09412,0.34902,0.66275}%
\definecolor{color3}{rgb}{0.92941,0.17647,0.18039}%
\definecolor{color4}{rgb}{0.00000,0.54902,0.27843}%
% include pgfplots
\usepackage{pgfplots}
\usepackage{pgfplotstable}
% custom settings
\pgfplotsset{compat=1.3}
\pgfplotsset{every axis/.append style={line width=1pt}} % line widths
\pgfplotsset{ % customized size (x1.3)
mysize/.style={
/pgfplots/width=312pt,
/pgfplots/height=269pt,
/pgfplots/max space between ticks=35}
}
\pgfplotsset{ % fer plot config
ferplotcfg/.style={
/pgfplots/xmin = 20,
/pgfplots/xmax = 32,
/pgfplots/ymin = 10e-3,
/pgfplots/ymax = 10e-1,
/pgfplots/grid = both,
minor grid style = {dashed},
major grid style = {dashed}}
}
\pgfplotsset{ % reference curve cfg
line_ref/.style={
color1,
/tikz/smooth,
/tikz/mark=none,
/tikz/style={dashed}}
}
\pgfplotsset{ % reference curve cfg
line_1/.style={
color2,
/tikz/smooth,
/tikz/mark=o}
}
\begin{document}
\centering
\begin{tikzpicture}
\begin{axis} [
title = FER Performance,
legend entries = {Reference, Fixed Point},
xlabel = {SNR (dB)}, ylabel = {FER},
ferplotcfg, mysize, ymode = log,
]
\addplot[line_ref] table[x=snr, y=reference, col sep=comma, row sep=crcr] {
snr,matlab_full_sim_fxp,reference\\
20,0.93682,0.92946\\
22,0.76335,0.73643\\
24,0.39312,0.3622\\
26,0.11584,0.10718\\
28,0.036743,0.029785\\
30,0.012695,0.010864\\
32,0.0064697,0.0039063\\};
\addplot[line_1] table[x=snr, y=matlab_full_sim_fxp, col sep=comma, row sep=crcr] {
snr,matlab_full_sim_fxp,reference\\
20,0.93682,0.92946\\
22,0.76335,0.73643\\
24,0.39312,0.3622\\
26,0.11584,0.10718\\
28,0.036743,0.029785\\
30,0.012695,0.010864\\
32,0.0064697,0.0039063\\};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
实现此目的的一种方法是结合pgfplots:将节点放置在特定的 x 位置上和将单位转换为坐标适用于 x 坐标,但不适用于 y 坐标首先将坐标放置在指定的 y 位置,然后计算差值:
\documentclass{standalone}
% include pgfplots
\usepackage{pgfplots}
\usetikzlibrary{intersections, calc}
% custom settings
\pgfplotsset{compat=newest}
\pgfplotsset{ % customized size (x1.3)
mysize/.style={
/pgfplots/width=312pt,
/pgfplots/height=269pt,
/pgfplots/max space between ticks=35}
}
\pgfplotsset{ % fer plot config
ferplotcfg/.style={
/pgfplots/xmin = 20,
/pgfplots/xmax = 32,
/pgfplots/ymin = 10e-3,
/pgfplots/ymax = 10e-1,
/pgfplots/grid = both,
%minor grid style = {dashed},
%major grid style = {dashed}
}
}
\pgfplotsset{ % reference curve cfg
line_ref/.style={
color1,
/tikz/smooth,
/tikz/mark=none,
/tikz/style={dashed}}
}
\pgfplotsset{ % reference curve cfg
line_1/.style={
color2,
/tikz/smooth,
/tikz/mark=o}
}
\makeatletter
\def\parsenode[#1]#2\pgf@nil{%
\tikzset{label node/.style={#1}}
\def\nodetext{#2}
}
\tikzset{
add node at y/.style 2 args={
name path global=plot line,
/pgfplots/execute at end plot visualization/.append={
\begingroup
\@ifnextchar[{\parsenode}{\parsenode[]}#2\pgf@nil
\path [name path global = position line #1]
({axis cs:0,#1}-|{rel axis cs:0,0}) --
({axis cs:0,#1}-|{rel axis cs:1,1});
\path [
name intersections={
of={plot line and position line #1},
name=intersection
},
] (intersection-1)
node [label node] {\nodetext};
\endgroup
}
}
}
\begin{document}
\centering
\begin{tikzpicture}
\begin{axis} [
ferplotcfg, mysize, ymode = log,
]
\addplot[black, add node at y={0.1}{[coordinate, name=pointA]}] table[x=snr, y=reference, col sep=comma, row sep=crcr] {
snr,matlab_full_sim_fxp,reference\\
20,0.93682,0.92946\\
22,0.76335,0.73643\\
24,0.39312,0.3622\\
26,0.11584,0.10718\\
28,0.036743,0.029785\\
30,0.012695,0.010864\\
32,0.0064697,0.0039063\\};
\addplot[blue, add node at y={0.1}{[coordinate, name=pointB]}] table[x=snr, y=matlab_full_sim_fxp, col sep=comma, row sep=crcr] {
snr,matlab_full_sim_fxp,reference\\
20,0.93682,0.92946\\
22,0.76335,0.73643\\
24,0.39312,0.3622\\
26,0.11584,0.10718\\
28,0.036743,0.029785\\
30,0.012695,0.010864\\
32,0.0064697,0.0039063\\};
\draw let \p1=(pointA), \p2=(pointB) in (pointA) -- (pointB) node [pos=0, left] {%
\pgfplotsconvertunittocoordinate{x}{\x1}%
\pgfplotscoordmath{x}{datascaletrafo inverse to fixed}{\pgfmathresult}%
\edef\valueA{\pgfmathresult}%
\pgfplotsconvertunittocoordinate{x}{\x2}%
\pgfplotscoordmath{x}{datascaletrafo inverse to fixed}{\pgfmathresult}%
\pgfmathparse{\pgfmathresult - \valueA}%
\pgfmathprintnumber{\pgfmathresult} dB
};
\end{axis}
\end{tikzpicture}
\end{document}