现在,它看起来是这样的:
我不喜欢方框与函数图相交。您如何重新定位它?
我看不到 y 轴的标签。它去哪儿了?您能把 y 轴稍微拉长一点,这样您就可以完全看到 y 轴的箭头吗?
代码如下:
\begin{figure}[!h]
\centering
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xlabel = $x$,
ylabel = {$f(x)$}
]
\addplot [
domain=-20:20,
samples=100,
color=blue,
]
{sin(deg(x)) / deg(x)};
\addlegendentry{$si(x) = sin(x) / x$}
\end{axis}
\end{tikzpicture}
\caption{}
\end{figure}
答案1
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xlabel = $x$,
ylabel = {$f(x)$},
ymax=1.2,
legend style={at={(0.65,.9)},anchor=north west},
]
\addplot [
domain=-20:20,
samples=200,
color=blue,
]
{sin(deg(x)) / (x)};
\addlegendentry{$si(x) = sin(x) / x$}
\end{axis}
\end{tikzpicture}
\end{document}
该si(x)
函数的最大值应为1
,因此函数应为{sin(deg(x)) / (x)}
而不是{sin(deg(x)) / deg(x)}
。此外,为了稍微放大 y 轴,我们可以添加ymax=1.2,
。对于图例定位,我们可以通过图例样式 调整位置legend style={at={(0.65,.9)},anchor=north west}
。
答案2
您可以使用xshift
和yshift
来定位图例。(虽然我使用了@Harish Kumar 的答案,但我用解决了这个问题shifts
)。
\documentclass[border={10pt}]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[%%%%%%%%%%%%%%%%%%%%%%%%%%%%
axis lines = middle,
xlabel = $x$,
ylabel = {$f(x)$},
enlarge y limits=upper,
yticklabel style = {font=\tiny},
xticklabel style = {font=\tiny},
legend style={xshift=1.5cm},
thick,
]%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\addplot
[%%%%%%%%%%%%%%%%%%%%%%%%%%%%
domain=-20:20,
samples=200,
color=blue,
]%%%%%%%%%%%%%%%%%%%%%%%%%%%%
{sin(deg(x)) / (x)};
\addlegendentry{$si(x) = sin(x) / x$}
\end{axis}
\end{tikzpicture}
\end{document}
答案3
您可以使用legend style
来重新定位图例。
legend style={at={(0.65,1)},anchor=north west},
此处坐标(0,0)
对应于左下角,(1,1)
对应于右上角。ylabel
隐藏在图例下,一旦移动图例,它就会变得可见。
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xlabel = $x$,
ylabel = {$f(x)$},
ytick=1,
legend style={at={(0.65,1)},anchor=north west},
enlarge y limits=upper, %% or enlargelimits=upper (if you want x axis too)
]
\addplot [
domain=-20:20,
samples=100,
color=blue,
]
{sin(deg(x)) / (x)};
\addlegendentry{$si(x) = sin(x) / x$}
\end{axis}
\end{tikzpicture}
\end{document}