我想画这幅画
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepackage{fouriernc}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
declare function={Y(\x)=-\x^3 +3 * \x^2 -4;},
axis line style = very thick,
axis lines = center,
xlabel=$x$,ylabel=$y$,
domain=-3.5:3.5,
ymin=-4.5,
ymax=1,
xmin=-1.2,
xmax=3.2,
samples=100,xtick distance=1,
ytick distance=2,unit vector ratio*=1 1 1,
width=11cm,
grid=major,
grid style={gray!30}
]
\addplot [black, thick] {Y(x)};
\addplot [black, mark=*,only marks,samples at={0,-1,1,-2,2,3}] {Y(x)};
\node at (axis cs:-0.25, -0.25) {$O$} ;
\pgfplotsinvokeforeach{1,3}{
\draw [blue,dashed](axis cs:#1,{Y(#1)}) -- (axis cs:#1,0);
}
\addplot[color=red,dashed,thick] coordinates {
(0 ,-2)
(1, -2) };
\addplot[color=red,dashed,thick] coordinates {
(0 ,-4)
(3, -4) };
\end{axis}
\end{tikzpicture}
\end{document}
我用手试了一下。
答案1
只需使用与蓝线相同的技术,添加
\draw [red,dashed](axis cs:#1,{Y(#1)}) -- (axis cs:0,{Y(#1)});
在循环。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepackage{fouriernc}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
declare function={Y(\x)=-\x^3 +3 * \x^2 -4;},
axis line style = very thick,
axis lines = center,
xlabel=$x$,ylabel=$y$,
domain=-3.5:3.5,
ymin=-4.5,
ymax=1,
xmin=-1.2,
xmax=3.2,
samples=100,xtick distance=1,
ytick distance=2,unit vector ratio*=1 1 1,
width=11cm,
grid=major,
grid style={gray!30}
]
\addplot [black, thick] {Y(x)};
\addplot [black, mark=*,only marks,samples at={0,-1,1,-2,2,3}] {Y(x)};
\node at (axis cs:-0.25, -0.25) {$O$} ;
\pgfplotsinvokeforeach{1,3}{
\draw [blue,dashed](axis cs:#1,{Y(#1)}) -- (axis cs:#1,0);
\draw [red,dashed](axis cs:#1,{Y(#1)}) -- (axis cs:0,{Y(#1)});
}
\end{axis}
\end{tikzpicture}
\end{document}