[Tex/LaTex] How to plot power functions

tikz-pgf

I want to plot y = x^n for n = 1,2,3,4,5 such that x in [0,1] and y in [0,1].

something like this image:

enter image description here

When I want to set xmin and xmax to restrict the domain, the error appear!
Where is wrong!

\documentclass{standalone}
‎\usepackage{tikz}‎
‎\usepackage[customcolors]{hf-tikz}‎‎‎
‎\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}‎[xmin = ‎0, ymin = 0, xmax = 1‎, ymax = 1‎]‎‎
\addplot[color=red,‎smooth‎] {x‎‎};‎‎
\addplot[color=red,‎smooth‎] {x^‎2‎};‎
\addplot[color=red,‎smoo‎th‎] {x^‎3‎};‎‎
\addplot[color=red,‎smooth‎] {x^‎4‎};‎
\end{axis}
\end{‎tikzpicture}‎
\end{document}

Best Answer

An alternative that uses \pgfplotsinvokeforeach to repeat the \addplot:

\documentclass[tikz,margin=2mm]{standalone}
\usepackage{pgfplots}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[domain=0:1,xmin=0,xmax=1,ymin=0,ymax=1]
            \pgfplotsinvokeforeach{1,2,...,15}{
                \addplot[red,smooth] {x^#1};
            }
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

And a bit more looking like your desired plot:

enter image description here

With:

\documentclass[tikz,margin=2mm]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            domain=0:1,
            axis equal,
            width=8cm,height=8cm,
            xmin=-0.05,xmax=1.05,
            ymin=-0.05,ymax=1.05,
            axis y line=middle,
            axis x line=middle,
            xtick=\empty,
            ytick=\empty,
        ]
            \pgfplotsinvokeforeach{1,...,20}{
                \addplot[red,smooth] {x^#1};
            }
            \draw[dashed] (1,0) -- (1,1);
            \draw[dashed] (0,1) -- (1,1);
        \end{axis}
    \end{tikzpicture}
\end{document}

And finally, an animation just for fun:

enter image description here

With:

\documentclass[tikz,margin=2mm]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}
    \foreach \numPowers in {1,...,20}{
        \begin{tikzpicture}
            \begin{axis}[
                domain=0:1,
                axis equal,
                width=8cm,height=8cm,
                xmin=-0.05,xmax=1.05,
                ymin=-0.05,ymax=1.05,
                axis y line=middle,
                axis x line=middle,
                xtick=\empty,
                ytick=\empty,
            ]
                \pgfplotsinvokeforeach{1,...,\numPowers}{
                    \addplot[red,smooth] {x^##1};
                }
                \draw[dashed] (1,0) -- (1,1);
                \draw[dashed] (0,1) -- (1,1);
            \end{axis}
        \end{tikzpicture}
    }
\end{document}

If anyone could comment on why I need ##1 instead of #1 in the \pgfplotsinvokeforeach when its enclosed in an ordinary \foreach, I would be very grateful.