网格超出了图形的范围,如下面的屏幕截图所示。我需要做哪些更改才能使网格与图形对齐?它应该只在坐标系内。
\usepackage{tkz-euclide}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}
\usetikzlibrary{positioning}
\begin{figure}
\begin{center}
\begin{tikzpicture}
\tkzInit[ymax=6, xmin=-6, ymin=-1]
\tkzGrid[xmax=6, ymax=6]
%\tkzAxeXY
\begin{axis}[
xlabel=x,
ylabel={y}, % max(0, x)
legend pos=north west,
legend cell align=left
]
\addplot[color=blue]{max(0, x)};
\addplot[color=red]{1/(1+exp(-x))};
\addplot[color=green]{(exp(x)-exp(-x))/(exp(x)+exp(-x))};
\addlegendentry{ReLu(x)}
\addlegendentry{sigmoid(x)}
\addlegendentry{tanh(x)}
\end{axis}
\end{tikzpicture}
\caption{Relu und sigmoid function}
\end{center}
\end{figure}
答案1
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=x,
ylabel=y,
legend pos=north west,
legend cell align=left,
grid,
ytick distance=1,
]
\addplot[blue] {max(0, x)};
\addplot[red] {1/(1+exp(-x))};
\addplot[green] {(exp(x)-exp(-x))/(exp(x)+exp(-x))};
\addlegendentry{ReLu(x)}
\addlegendentry{sigmoid(x)}
\addlegendentry{tanh(x)}
\end{axis}
\end{tikzpicture}
\end{document}