如何用 latex 绘制这个流线图 xy=C?我附上了我的代码,但它没有生成想要的图形。
这是我的代码
\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{shapes.symbols}
\makeatletter
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
xlabel = $x$,
ylabel = {$f(x)$},
]
%Below the red is defined
\addplot [
domain=-2:2,
samples=20,
color=red,
]
{ 1/x};
\addlegendentry{$xy=-1$}
%Here is defined
\addplot [
domain=-2:2,
samples=20,
color=green,
]
{-3/x};
\addlegendentry{$xy=3$}
\addplot [
domain=-2:2,
samples=20,
color=green,
]
{5/x};
\addlegendentry{$xy=-5$}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
这里我提出了一种使用非线性间距的解决方案,该解决方案也适用于相当低的值C或“高”值Max
(见代码)。例如,分别使用值 0.1、0.2 和 0.3 或Max
值 10 进行测试。每条(全)线仅使用 22 个样本,它们就可以完美运行。
有关解决方案如何运作的详细信息,请查看代码中的注释。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
% load library to use a "cool" `cycle list'
\usetikzlibrary{
pgfplots.colorbrewer,
}
\pgfplotsset{
% use this `compat' level or higher to use the advanced positioning
% for the axis labels
compat=1.3,
/pgf/declare function={
% declare a variable to store the min and max value for the function
Max=5;
% declare the function to use
f(\C,\x) = \C/\x;
%
% -----------------------------------------------------------------
% calculate the lower and upper boundaries (the domain values)
lb(\C) = sqrt(\C);
ub = Max;
%
%%% non-linear spacing: <https://stackoverflow.com/a/39140096/5776000>
% "non-linearity factor"
a = 1.0;
% function to use for the non-linear spacing
Y(\x) = exp(a*\x);
% rescale to former limits
X(\C,\x) = (Y(\x) - Y(lb(\C)))/(Y(ub) - Y(lb(\C))) * (ub - lb(\C)) + lb(\C);
% -----------------------------------------------------------------
},
% load a "cool" cycle list
cycle list/Blues-5,
}
\begin{document}
\begin{tikzpicture}[
% define a style for the plot labels
Label/.style={
node font=\tiny,
black,
pos=#1,
},
]
\begin{axis}[
% axis lines should be centered
axis lines=center,
% set axis labels
xlabel={$x$},
ylabel={$f(x)$},
% set them right and above the axis lines, respectively
xlabel style={
at={(xticklabel* cs:1)},
anchor=west,
},
ylabel style={
at={(yticklabel* cs:1)},
anchor=south,
},
% axis unit ratio should be the same for both axis lines
axis equal image=true,
% don't show any ticks (and tick labels)
ticks=none,
% the lines should be `smooth'
smooth,
% when using the non-linear spacing approach you don't need that much samples
samples=11,
% % ---------------------------------------------------------------------
% % for debugging purposes only
% % ---------------------------------------------------------------------
% % add small marks to the lines
% every axis plot post/.append style={
% mark=*,
% mark size=0.3pt,
% },
% % ---------------------------------------------------------------------
]
% first draw the lines for $C = 0$
\pgfplotsinvokeforeach {0} {
% the horizontal line
% used `forget plot' so the `cycle list index' isn't increased
\addplot+ [domain=-Max:Max,forget plot,>->] {#1}
node [Label=0.4,above] {$C = #1$}
node [Label=0.6,above] {$C = #1$}
;
% the vertical line
\addplot+ [domain=-Max:Max,>->] (#1,x)
node [Label=0.25,left] {$#1$}
node [Label=0.75,left] {$#1$}
;
}
% define a length for the shift of the plot labels
% (using "above left" and similar causes the labels to be
% "too far away" from the plot they belong to and are almost
% *in* the next plot)
\pgfmathsetlengthmacro{\Shift}{3pt}
% define a factor to enlarge the domain to get the parabolic effect
% of the end points of the different "C" plots
% (for linear spacing use the value 0.25)
\pgfmathsetmacro{\Factor}{0.01}
% now draw plots for $C > 0$
\pgfplotsinvokeforeach {1,2,3} {
% to do so we split each line into two part
% this is advantage because of the symmetry of the lines and
% thus we can avoid the need for a lot of "samples" in the steep
% parts of the curve.
%
% -----------------------------------------------------------------
% linear spacing approach
% -----------------------------------------------------------------
% % quadrant I
% \addplot+ [domain=+sqrt(#1):{Max+\Factor*#1},forget plot,->] {f(#1,x)}
% node [Label=0,shift={(\Shift,\Shift)}] {$+#1$};
% % the steep parts can be drawn using a parametric plot
% \addplot+ [domain=+sqrt(#1):{Max+\Factor*#1},forget plot,-<] ({f(#1,x)},x);
%
% % quadrant II
% \addplot+ [domain={-Max-\Factor*#1}:-sqrt(#1),forget plot,>-] {-f(#1,x)}
% node [Label=1,shift={(-\Shift,\Shift)}] {$-#1$};
% \addplot+ [domain=+sqrt(#1):{Max+\Factor*#1},forget plot,->] ({-f(#1,x)},x);
%
% % quadrant III
% \addplot+ [domain={-Max-\Factor*#1}:-sqrt(#1),forget plot,>-] {f(#1,x)}
% node [Label=1,shift={(-\Shift,-\Shift)}] {$+#1$};
% \addplot+ [domain={-Max-\Factor*#1}:-sqrt(#1),forget plot,<-] ({f(#1,x)},x);
%
% % quadrant IV
% \addplot+ [domain=+sqrt(#1):{Max+\Factor*#1},forget plot,->] {-f(#1,x)}
% node [Label=0,shift={(\Shift,-\Shift)}] {$-#1$};
% \addplot+ [domain={-Max-\Factor*#1}:-sqrt(#1),>-] ({-f(#1,x)},x);
% -----------------------------------------------------------------
% non-linear spacing approach
% -----------------------------------------------------------------
% quadrant I
\addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,forget plot,->] ({X(#1,x)},{f(#1,X(#1,x))})
node [Label=0,shift={(\Shift,\Shift)}] {$+#1$};
% the steep parts can be drawn using a parametric plot
\addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,forget plot,-<] ({f(#1,X(#1,x))},{X(#1,x)});
% quadrant II
\addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,forget plot,-<] ({-X(#1,x)},{f(#1,X(#1,x))})
node [Label=0,shift={(-\Shift,\Shift)}] {$-#1$};
\addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,forget plot,->] ({-f(#1,X(#1,x))},{X(#1,x)});
% quadrant III
\addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,forget plot,-<] ({-X(#1,x)},{-f(#1,X(#1,x))})
node [Label=0,shift={(-\Shift,-\Shift)}] {$+#1$};
\addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,forget plot,->] ({-f(#1,X(#1,x))},-{X(#1,x)});
% quadrant IV
\addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,forget plot,->] ({X(#1,x)},{-f(#1,X(#1,x))})
node [Label=0,shift={(\Shift,-\Shift)}] {$-#1$};
\addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,-<] ({f(#1,X(#1,x))},{-X(#1,x)});
}
\end{axis}
\end{tikzpicture}
\end{document}
答案2
@Barry——您的停滞点流中发生了很多事情,但这是一个开始,需要在标签和箭头方面做更多的工作。
\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{shapes.symbols}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=center,
axis y line=center,
xlabel = $x$,
ylabel = {$y$},
ymax=3,
ymin=-3,
ticks=none,
]
\node[right] at (-2,2) {$C=-2$};
\node[right] at (-0.7,0.2) {$C=0$};
%Below the red is defined
\addplot [
domain=-2:-0.1,
samples=50,
color=red,
]
{1/x};
\addplot [
domain=0.1:2,
samples=50,
color=red,
]
{(1/x)};
\addplot [
domain=-2:-0.1,
samples=50,
color=red,
]
{ -1/x};
\addplot [
domain=0.1:2,
samples=100,
color=red,
]
{(-1/x)};
% Now the teal
\addplot [
domain=-2.:-0.1,
samples=50,
color=teal,
]
{(0.5/x)};
\addplot [
domain=0.1:2,
samples=50,
color=teal,
]
{(0.5/x)};
\addplot [
domain=-2:-0.1,
samples=100,
color=teal,
]
{ -0.5/x};
\addplot [
domain=0.1:2,
samples=50,
color=teal,
]
{(-0.5/x)};
%Here green is defined
\addplot [
domain=-2:-0.1,
samples=50,
color=green,
]
{-2/x};
\addplot [
domain=-2:-0.1,
samples=50,
color=green,
]
{2/x};
\addplot [
domain=0.1:2,
samples=50,
color=green,
]
{-2/x};
\addplot [
domain=0.1:2,
samples=50,
color=green,
]
{-2/x};
\end{axis}
\end{tikzpicture}
\end{document}