我怎样才能用 tikz 绘制这样的图形?
函数:f(x) = x^2 + 0.25、f(x) = x^2 + 0.1 和 f(x) = x^2 + 0.4
它们的对角线均为 y=x。
我开始于:
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain = -.6:1,
samples = 50,
axis x line = center,
axis y line = center,
xlabel = {$x$},
ylabel = {$y$},
ticks = none
]
\addplot[blue] {x*x + 0.25};
\addplot[black] {x}
\end{axis}
\end{tikzpicture}
\end{document}
但我不知道我错过了什么
答案1
您已经非常接近了,只缺少几行。更改函数以显示其他两个图很容易。
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain = -.6:1,
samples = 50,
axis x line = center,
axis y line = center,
xlabel = {$x$},
ylabel = {$y$},
try min ticks = 10,
scale only axis,
]
\addplot[blue] {x*x + 0.25};
\addplot[black] {x};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
如果我可以在@bmv 的答案上添加一个小樱桃,你可能会受益于像这样声明你的函数
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
% Declare the functions you need
\tikzset{
declare function={
f(\x)=\x*\x + 0.25;
g(\x)= f(\x) - 0.15;
h(\x)= f(\x) + 0.15;
}
}
\begin{axis}[
domain = -.6:1,
samples = 50,
axis x line = center,
axis y line = center,
xlabel = {$x$},
ylabel = {$y$},
try min ticks = 10,
scale only axis,
]
% easy to resuse
\addplot[blue] {f(x)};
% \addplot[red] {g(x)};
% \addplot[green] {h(x)};
\addplot[black] {x};
\end{axis}
\end{tikzpicture}
\end{document}