![我怎样才能自动绘制此图中的红色虚线?](https://linux22.com/image/349642/%E6%88%91%E6%80%8E%E6%A0%B7%E6%89%8D%E8%83%BD%E8%87%AA%E5%8A%A8%E7%BB%98%E5%88%B6%E6%AD%A4%E5%9B%BE%E4%B8%AD%E7%9A%84%E7%BA%A2%E8%89%B2%E8%99%9A%E7%BA%BF%EF%BC%9F.png)
我想画这幅画
\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}