我想延长 y 轴,以便图表上的最大值不在 y 轴箭头的尖端。(出于同样的原因,我也想延长 x 轴)。请参见下面的示例: 我想要的应该是这样的(可以在以下位置找到:https://tikz.dev/dv-axes)
我还想将如图所示的刻度添加至我的示例中。
谢谢!(抱歉图片太大了,我不知道如何在这里缩放它们)
代碼:
\documentclass[11pt]{article}
\usepackage[a4paper,hmargin=1in,top=1.2in,bottom=1in]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-5, xmax=5,
ymin=-0.5, ymax=5,
ticks=none,
axis y line=middle,
axis x line=middle,
axis line style=thick,
ytick pos=upper,
xlabel=$(1)$, ylabel=$(2)$,
x label style={anchor=west},
y label style={anchor=south},
]
\addplot [
ultra thick,
domain=-10:10,
color=black!50!green,
smooth
]
{0.3333333*x^2};
%\addlegendentry{\(\frac{1}{3}x^2\)}
\addplot [
ultra thick,
domain=-10:10,
color=black!20!blue,
smooth
]
{x^2};
%\addlegendentry{\(x^2\)}
\addplot [
ultra thick,
domain=-10:10,
color=black!10!red,
smooth
]
{3*x^2};
%\addlegendentry{\(3x^2\)}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=8cm, height=10cm,
xmin=-20, xmax=20,
ymin=0, ymax=40,
axis lines=middle, axis line style=thick,
xlabel=$x$, ylabel=$f(x)$,
xlabel style={anchor=west}, ylabel style={anchor=south},
enlarge x limits={abs=2}, enlarge y limits=0.1,
]
\clip (-20,0) rectangle (20,40);
\addplot[
red!90!black, ultra thick,
domain=-20:20,
smooth
] {0.5*x^2};
\addplot[
green!50!black, ultra thick,
domain=-20:20,
smooth,
] {0.1*x^2};
\addplot[
blue!80!black, ultra thick,
domain=-20:20,
smooth
] {0.05*x^2};
\end{axis}
\end{tikzpicture}
\end{document}
您还可以使用该选项axis line style={shorten >=-10pt, shorten <=-10pt}
简单地将轴线画得更长。这有一些限制 - 此扩展中没有刻度、没有网格,并且需要手动调整标签等。
答案2
您可以使用restrict y to domain=0:5
和axis line style={-to}
\begin{tikzpicture}
\begin{axis}[
xmin=-5, xmax=5,
ymin=-0.5, ymax=6,
ticks=none,
axis y line=middle,
axis x line=middle,
axis line style={-to},
restrict y to domain=0:5,
axis line style=thick,
ytick pos=upper,
xlabel=$(1)$, ylabel=$(2)$,
x label style={anchor=west},
y label style={anchor=south},
]
\addplot [
ultra thick,
domain=-5:5,
color=black!50!green,
smooth,
samples=750,
]
{x^2/3};
%\addlegendentry{\(\frac{1}{3}x^2\)}
\addplot [
ultra thick,
domain=-5:5,
color=black!20!blue,
smooth,
samples=500,
]
{x^2};
%\addlegendentry{\(x^2\)}
\addplot [
ultra thick,
domain=-5:5,
color=black!10!red,
smooth,
samples=1100,
]
{3*x^2};
%\addlegendentry{\(3x^2\)}
\end{axis}
\end{tikzpicture}