我需要在 TikZ 中完成此操作。有人能帮帮我吗?我知道如何完成轴,但曲线本身对我来说很难:
梅威瑟:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}%http://www.ctan.org/pkg/pgfplots
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
domain = -.5:3,
samples = 50,
axis x line = center,
axis y line = center,
xlabel = {$x$},
ylabel = {$y$},
ticks = none
]
\addplot[blue] {abs(x-2)} [yshift=3pt] node[pos=.95,left] {$y=|x-2|$};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
绘制图表的代码pgfplots
比纯代码稍微复杂一些tikz
:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
axis lines = center,
xlabel = {$T$},
ylabel = {$E(T)$},
label style = {anchor=north east},
xmin = 0, xmax=5, ymin=0, ymax=1,
ticks = none,
enlargelimits=false,
clip=false,
%
domain = 1:4,
samples = 50,
no marks
]
\addplot {1/x};
\draw[red, densely dashed] (0,1/2) node[left] {$E_0$} -| (2,0) node[below] {$T_0$};
\draw[blue, densely dashed] (0,1/3) node[left] {$E_1$} -| (3,0) node[below] {$T_1$};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
对于简单的事情,你可以使用裸TikZ,而无需pgfplots
代码
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[
mark point at x/.style 2 args={
densely dotted,
insert path={(#1, 0) |- (0, {f(#1)})
node[black, at start, below] {$T_{#2}$}
node[black, at end, left] {$E_{#2}$}}
},
declare function={f(\x)=1/\x;}
]
\draw[<->] (up:3) |- (right:5);
\draw plot [smooth, domain=.34:4.9] (\x, {f(\x)});
\begin{scope}[nodes={font=\small}]
\draw[blue, mark point at x={2}{1}];
\draw[red, mark point at x={1}{0}];
\node[below] at (right:4) {Token};
\node[left] at (up:2) {Eth};
\end{scope}
\end{tikzpicture}
\end{document}