我想在平面上生成四条双曲线并标记它们。我使用以下代码(抱歉它很长,请随意缩短它...):
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
every inner x axis line/.append style={help lines},
every inner y axis line/.append style={help lines},
every axis label/.append style={gray},
axis lines = center,
xtick = \empty,ytick=\empty,
xlabel={\tiny$x$},ylabel={\tiny$y$},
enlargelimits=auto
]
\addplot+[mark=none,red!80!black] function[raw gnuplot] {
set xrange [-5:5];
set yrange [-5:5];
set contour base;
set cntrparam levels discrete 0.0;
unset surface;
set view map;
set isosamples 70;
splot x*x/0.5-y*y/1.25-1;
} node[pos=0.4999,sloped,below]{\tiny$\frac{x^2}{a^2} - \frac{y^2}{b^2} = 1$};
\addplot+[mark=none,red!90] function[raw gnuplot] {
set xrange [-5:5];
set yrange [-5:5];
set contour base;
set cntrparam levels discrete 0.0;
unset surface;
set view map;
set isosamples 70;
splot x*x/0.5-y*y/1.25+1;
} node[pos=0.95,sloped,below]{\tiny$\frac{x^2}{a^2} - \frac{y^2}{b^2} = -1$};
\addplot+[mark=none,blue!80!black] function[raw gnuplot] {
set xrange [-5:5];
set yrange [-5:5];
set contour base;
set cntrparam levels discrete 0.0;
unset surface;
set view map;
set isosamples 70;
splot x*x/1.25-y*y/0.5-1;
} node[pos=0.9999,sloped,below]{\tiny$\frac{x^2}{b^2} - \frac{y^2}{a^2} = 1$};
\addplot+[mark=none,blue!80] function[raw gnuplot] {
set xrange [-5:5];
set yrange [-5:5];
set contour base;
set cntrparam levels discrete 0.0;
unset surface;
set view map;
set isosamples 70;
splot x*x/1.25-y*y/0.5+1;
} node[pos=0.6,sloped,below]{\tiny$\frac{x^2}{b^2} - \frac{y^2}{a^2} = -1$};
\end{axis}
\end{tikzpicture}
\end{document}
我的问题是标签的位置。我试图调整键pos=
,但始终找不到正确的位置。我猜是因为它的分支行为,它出乎意料地跳来跳去。我只能找到四个中三个令人满意的定位。标记双曲线的正确方法是什么?我考虑过使用绝对定位,但我希望有更好的解决方法。
答案1
您可以使用以下方法pgfplots:将节点放置在特定的 x 位置上将节点放置在图上的特定 x 和 y 位置:
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.9}
\usetikzlibrary{intersections}
\begin{document}
\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
}
},
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-1]
({axis cs:0,#1}-|{rel axis cs:0,0}) --
({axis cs:0,#1}-|{rel axis cs:1,1});
\path [yshift=1pt, name path global = position line #1-2]
({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-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
}
}
}
\begin{tikzpicture}
\begin{axis}[
every inner x axis line/.append style={help lines},
every inner y axis line/.append style={help lines},
every axis label/.append style={gray},
axis lines = center,
xtick = \empty,ytick=\empty,
xlabel={\tiny $x$},ylabel={\tiny $y$},
enlargelimits=auto
]
\addplot+[mark=none,red!80!black,
add node at y={-4}{[red!80!black, sloped, above, inner sep=1pt]{\tiny $\frac{x^2}{a^2} - \frac{y^2}{b^2} = 1$}}
] function[raw gnuplot] {
set xrange [-5:5];
set yrange [-5:5];
set contour base;
set cntrparam levels discrete 0.0;
unset surface;
set view map;
set isosamples 70;
splot x*x/0.5-y*y/1.25-1;
};
\addplot+[mark=none,red!90,
add node at y={3}{[red!90, sloped, above, inner sep=1pt]{\tiny $\frac{x^2}{a^2} - \frac{y^2}{b^2} = -1$}}
] function[raw gnuplot] {
set xrange [-5:5];
set yrange [-5:5];
set contour base;
set cntrparam levels discrete 0.0;
unset surface;
set view map;
set isosamples 70;
splot x*x/0.5-y*y/1.25+1;
};
\addplot+[mark=none,blue!80!black,
add node at x={-4}{[blue!80!black, sloped, below, inner sep=1pt]{\tiny $\frac{x^2}{b^2} - \frac{y^2}{a^2} = 1$}}
] function[raw gnuplot] {
set xrange [-5:5];
set yrange [-5:5];
set contour base;
set cntrparam levels discrete 0.0;
unset surface;
set view map;
set isosamples 70;
splot x*x/1.25-y*y/0.5-1;
};
\addplot+[mark=none,blue!80,
add node at x={4}{[blue!80, sloped, above, inner sep=1pt]{\tiny $\frac{x^2}{b^2} - \frac{y^2}{a^2} = -1$}}
] function[raw gnuplot] {
set xrange [-5:5];
set yrange [-5:5];
set contour base;
set cntrparam levels discrete 0.0;
unset surface;
set view map;
set isosamples 70;
splot x*x/1.25-y*y/0.5+1;
};
\end{axis}
\end{tikzpicture}
\end{document}