我尝试使用以下代码在特定符号坐标 (x1, C(x1)) 处绘制辅助线:
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$C$},
axis lines=center,
symbolic x coords={0,$x_1$,$x_2$,$x_3$,$x_4$},
xmin={0},
xmax={$x_4$},
xtickmax={$x_3$},
xtick distance=1,
symbolic y coords={0,$C(x_1)$,$C(x_2)$,$C(x_3)$,$C(x_4)$},
ymin={0},
ymax={$C(x_4)$},
ytickmax={$C(x_3)$},
ytick distance=1]
\addplot [color=red,mark=*] coordinates {($x_1$,{$C(x_1)$})};
\draw [dashed,help lines] (0,{$C(x_1)$}) -| ($x_1$,0);
\end{axis}
\end{tikzpicture}
但声明:
\draw [dashed,help lines] (0,{$C(x_1)$}) -| ($x_1$,0);
是错误的。我的目标是:
感谢您的帮助。
答案1
axis cs
即使您使用 1.11 或更新版本,也必须明确提供符号坐标。
\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$C$},
axis lines=center,
symbolic x coords={0,$x_1$,$x_2$,$x_3$,$x_4$},
xmin={0},
xmax={$x_4$},
xtickmax={$x_3$},
xtick distance=1,
symbolic y coords={0,$C(x_1)$,$C(x_2)$,$C(x_3)$,$C(x_4)$},
ymin={0},
ymax={$C(x_4)$},
ytickmax={$C(x_3)$},
ytick distance=1]
\addplot [color=red,mark=*] coordinates {($x_1$,{$C(x_1)$})};
\draw [dashed,help lines] (axis cs:0,{$C(x_1)$}) -| (axis cs:$x_1$,0);
\end{axis}
\end{tikzpicture}
\end{document}
结果:
没有符号坐标:
\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
axis lines=center,
xmin=0,xmax=4,
xtick={1,...,3},
xticklabel={$x_{\pgfmathprintnumber[precision=0]{\tick}}$},
ymin=0,ymax=4,
ytick={1,...,3},
yticklabel={$C(x_{\pgfmathprintnumber[precision=0]{\tick}})$},
]
\addplot [color=red,mark=*] coordinates {(1,1)};
\draw [dashed,help lines] (0,1) -| (1,0);
\end{axis}
\end{tikzpicture}
\end{document}
结果和上面一样。