How do I make a rectangular plot with annotations?
Use width
and height
to set the dimensions of the axis, adjust to your liking. For the labels, you can first add a coordinate
at an appropriate pos
at each \addplot
, e.g. \addplot {..} coordinate[pos=0.6] (foo);
, then add clip mode=individual
to the axis
options, and finally use \node [pin={30:$C=x$}] at (foo) {};
to make the pin with label.
In the code below I used a loop, as the code becomes a lot shorter, but you can of course write out everything if you find it clearer.
\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}
\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}
A way to do this with MetaPost, for whom it may interest. The problematic pointing segments are build thanks to the handy cutafter
macro. As its names suggests, it doesn't draw the part of a given path located after its intersection with another given path.
The MetaPost code is included in a LuaLaTeX program for convenience.
\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}