在图的右边添加注释

在图的右边添加注释

我想在绘图区域外、右轴以及取决于绘图坐标的位置添加注释。

我可以使用自定义刻度或手动添加节点来实现这一点,但在后一种情况下,我必须提供代码after end axis或禁用剪辑(这可能会产生其他不良影响,例如...不剪辑)。到目前为止,我发现的最佳解决方案是:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}

\def\angs{0.529177208354}
\def\ev{27.2113838656}

\begin{axis}[
  xmin = 2,
  xmax = 4,
  ymin = 0,
  ymax = 5,
  xlabel = {$r$},
  ylabel = {$E$},
  after end axis/.code = {
    \path (axis cs:4.0,1.6) node[right] {B3LYP};
  } 
] 

\def\ezero{-1.11}
\addplot+ table[x expr={\thisrowno{0}*2*\angs}, y expr={(\thisrowno{2}-\ezero)*\ev}] {data.dat};

\end{axis}

\end{tikzpicture}
\end{document}

它存在两个问题:

  1. 我想在旁边给出代码\addplot,而不是在axis选项中。
  2. 我希望自动计算坐标(特别是纵坐标)。

显示以下data.dat文件的结果:

2.0 -13840.8142382 -0.8142382
2.1 -13840.9230067 -0.9230067
2.2 -13840.9967745 -0.9967745
2.3 -13841.0454158 -1.0454158
2.4 -13841.0762998 -1.0762998
2.5 -13841.0944654 -1.0944654
2.6 -13841.1038383 -1.1038383
2.7 -13841.1070221 -1.1070221
2.8 -13841.1059849 -1.1059849
2.9 -13841.1022183 -1.1022183
3.0 -13841.0966277 -1.0966277
3.1 -13841.0899909 -1.0899909
3.2 -13841.0828688 -1.0828688
3.3 -13841.0755779 -1.0755779
3.4 -13841.0687860 -1.0687860
3.5 -13841.0626993 -1.0626993
3.6 -13841.0572297 -1.0572297
3.7 -13841.0527363 -1.0527363
3.8 -13841.0493218 -1.0493218
3.9 -13841.0467450 -1.0467450
4.0 -13841.0447949 -1.0447949
4.1 -13841.0433105 -1.0433105
4.2 -13841.0421877 -1.0421877
4.3 -13841.0413471 -1.0413471
4.4 -13841.0407159 -1.0407159
4.5 -13841.0402353 -1.0402353
4.6 -13841.0398710 -1.0398710
4.7 -13841.0395993 -1.0395993
4.8 -13841.0393960 -1.0393960
4.9 -13841.0392397 -1.0392397
5.0 -13841.0391747 -1.0391747

试验地块

PS. 另一个附加问题:有没有办法使用第二列的数字,而不是第三列(即第二列 + 13840),而不会丢失精度?

答案1

您可以使用以下代码pgfplots - 将节点放置在绘图的 x 坐标上为此。它允许你写

\addplot+ [add node at x={4}{[anchor=west]B3LYP}] table ...

(或者

`\addplot+ [add node at x={\pgfkeysvalueof{/pgfplots/xmax}}{[anchor=west]B3LYP}] table...

如果你不想手动指定 x 值)来获取

请注意,如果您使用没有标记的绘图样式,则必须进行设置,clip mode=individual以防止节点被剪掉。

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}

\makeatletter
\def\parsenode[#1]#2\pgf@nil{%
    \tikzset{label node/.style={#1}}
    \def\nodetext{#2}
}

\tikzset{
    add node at x/.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-1]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [xshift=1pt, name path global = position line #1-2]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [
                name intersections={
                    of={plot line and position line #1-1},
                    name=left intersection
                },
                name intersections={
                    of={plot line and position line #1-2},
                    name=right intersection
                },
                label node/.append style={pos=1}
            ] (left intersection-1) -- (right intersection-1)
            node [label node]{\nodetext};
            \endgroup
        }
    }
}

\def\angs{0.529177208354}
\def\ev{27.2113838656}

\begin{axis}[
  xmin = 2,
  xmax = 4,
  ymin = 0,
  ymax = 5,
  xlabel = {$r$},
  ylabel = {$E$},
  cycle list name = linestyles*,
  clip mode=individual
] 

\def\ezero{-1.11}
\addplot+ [,add node at x={4}{[anchor=west,]B3LYP}]
 table[x expr={\thisrowno{0}*2*\angs}, y expr={(\thisrowno{2}-\ezero)*\ev}] {
2.0 -13840.8142382 -0.8142382
2.1 -13840.9230067 -0.9230067
2.2 -13840.9967745 -0.9967745
2.3 -13841.0454158 -1.0454158
2.4 -13841.0762998 -1.0762998
2.5 -13841.0944654 -1.0944654
2.6 -13841.1038383 -1.1038383
2.7 -13841.1070221 -1.1070221
2.8 -13841.1059849 -1.1059849
2.9 -13841.1022183 -1.1022183
3.0 -13841.0966277 -1.0966277
3.1 -13841.0899909 -1.0899909
3.2 -13841.0828688 -1.0828688
3.3 -13841.0755779 -1.0755779
3.4 -13841.0687860 -1.0687860
3.5 -13841.0626993 -1.0626993
3.6 -13841.0572297 -1.0572297
3.7 -13841.0527363 -1.0527363
3.8 -13841.0493218 -1.0493218
3.9 -13841.0467450 -1.0467450
4.0 -13841.0447949 -1.0447949
4.1 -13841.0433105 -1.0433105
4.2 -13841.0421877 -1.0421877
4.3 -13841.0413471 -1.0413471
4.4 -13841.0407159 -1.0407159
4.5 -13841.0402353 -1.0402353
4.6 -13841.0398710 -1.0398710
4.7 -13841.0395993 -1.0395993
4.8 -13841.0393960 -1.0393960
4.9 -13841.0392397 -1.0392397
5.0 -13841.0391747 -1.0391747
};

\end{axis}

\end{tikzpicture}
\end{document}

相关内容