我想绘制以下内容
但我无法添加细节(C 及其指向部分),也无法保持 $x$ 轴短于 $y$ 轴(如图所示)。这是我的 MWE
\documentclass[tikz]{standalone}
\usepackage{pgfplots,kpfonts}
\usetikzlibrary{
calc,angles,positioning,intersections,quotes,decorations.markings,backgrounds,patterns,
decorations.pathreplacing % <- added
}
\tikzset{
point/.style={circle,draw=black,inner sep=0pt,minimum size=3pt}
}
\pgfplotsset{
soldot/.style={color=blue,only marks,mark=*}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmax=1.5,
xmin=-1.5,
axis equal,
xtick={0},
ytick={-2,-1,1,2},
axis lines =middle, xlabel=$x$, ylabel=$y$,
every axis x label/.style={at=(current axis.right of origin),anchor=west}
]
\addplot [blue, smooth,domain=-2:2] {x^3+2};
\addplot [blue, smooth,domain=-2:2] {x^3+1};
\addplot [blue, smooth,domain=-2:2] {x^3};
\addplot [blue, smooth,domain=-2:2] {x^3-1};
\addplot [thick, red, smooth,domain=-2:2] {x^3-2};
\addplot[soldot,red]coordinates {(1,-1)} node [anchor=west,text=black] {$(1,-1)$};
\node at (axis cs:-0.6,-0.3) [anchor=west] {$0$};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
使用width
和height
来设置轴的尺寸,根据您的喜好进行调整。对于标签,您可以先在每个 处coordinate
添加,例如,然后添加到选项中,最后使用来制作带标签的图钉。pos
\addplot
\addplot {..} coordinate[pos=0.6] (foo);
clip mode=individual
axis
\node [pin={30:$C=x$}] at (foo) {};
在下面的代码中我使用了循环,因为代码变得更短,但是如果您发现更清楚的话当然可以写出所有内容。
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\tikzset{
point/.style={circle,draw=black,inner sep=0pt,minimum size=3pt}
}
\pgfplotsset{
soldot/.style={color=blue,only marks,mark=*}
}
\begin{document}
\begin{tikzpicture}[every pin edge/.style={black,thin}, pin distance=7mm]
\begin{axis}[
% set size of axis
width=7cm,height=10cm,
% disable clipping of \node, \draw, etc., while keeping clipping of \addplot
clip mode=individual,
xmax=1.5,
xmin=-1.5,
axis equal,
xtick={0},
ytick={-2,-1,1,2},
axis lines =middle, xlabel=$x$, ylabel=$y$,
every axis x label/.style={at=(current axis.right of origin),anchor=west}
]
\pgfplotsinvokeforeach{-2,...,2}{
\ifnum #1 = -2
\addplot [thick, red, smooth,domain=-2:2] {x^3+#1} coordinate[pos=0.68] (n#1);
\else
\addplot [blue, smooth,domain=-2:2] {x^3+#1} coordinate[pos=0.64-0.02*#1] (n#1);
\fi
\node[inner sep=1pt,pin={[anchor=west]30:$C=#1$}] at (n#1) {};
}
\addplot[soldot,red]coordinates {(1,-1)} node [anchor=west,text=black] {$(1,-1)$};
\node at (axis cs:-0.6,-0.3) [anchor=west] {$0$};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
\documentclass[pstricks]{standalone}
\usepackage{kpfonts}
\usepackage{pst-plot,multido}
\begin{document}
\begin{pspicture}(-2,-5)(3.5,5)
\psaxes[labels=y]{->}(0,0)(-2,-5)(2,5)
\multido{\iA=-2+1,\rA=1.1+0.05}{5}{%
\psplot[linecolor=\ifnum\iA>-2 blue\else red\fi,algebraic,linewidth=1.5pt]{-1.5}{2}{x^3+\iA}%
\psline(*\rA\space x^3+\iA)(!2 \rA\space 3 exp \iA\space add)
\uput[0](!2 \rA\space 3 exp \iA\space add){$C=\iA$}}
\psdot[linecolor=red,dotscale=1.5](1,-1)
\uput[-45](1,-1){$(1,-1)$}
\end{pspicture}
\end{document}
答案3
一种使用 MetaPost 实现此目的的方法,可能会引起人们的兴趣。有问题的指向段是通过方便的cutafter
宏构建的。顾名思义,它不会绘制给定路径与另一给定路径相交之后的部分。
为了方便起见,MetaPost 代码包含在 LuaLaTeX 程序中。
\documentclass[border=3mm]{standalone}
\usepackage{luatex85, luamplib}
\mplibsetformat{metafun}
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
picture curves; path graph[];
u := cm; xmin = -2.5; xmax = 3; xstep := .1; ymax = -ymin = 4;
beginfig(1);
% Axes
drawarrow (xmin*u, 0) -- (xmax*u, 0);
label.bot("$x$", (xmax*u, 0));
drawarrow (0, ymin*u) -- (0, ymax*u);
label.ulft("$y$", (0, ymax*u));
% Clipped cubic curves
curves = image(
for C = -2 upto 2:
graph[C] = function(2, "x", "x**3+(" & decimal C & ")", xmin, xmax, xstep) scaled u;
draw graph[C] withcolor if C = -2: red else: blue fi;
label.ulft("$" & decimal C & "$", (0, C*u));
endfor;);
clip curves to
unitsquare xyscaled (xmax-xmin, ymax-ymin) shifted (xmin, ymin) scaled u;
draw curves;
% Equation, and label for (1, 1)
z = (u, -u); drawdot z withpen pencircle scaled 3bp withcolor red;
label.rt("$(1, -1)$", z); label.rt("$y = x^3 + C$", u*(xmin, .75ymax));
% Labels C and their segments
for C = -2 upto 2:
pair loc; loc = u*(1.75+.06(C+2), 2.5+.6C);
label.rt("$C = " & decimal C & "$", loc);
draw (loc -- loc+infinity*dir (-165)) cutafter graph[C] ;
endfor;
endfig;
\end{mplibcode}
\end{document}