# How to plot functions like ‎‎‎‎‎x=f(y)‎‎ using TikZ?

To plot functions in the form of y=f(x), we simply write‎

‎\draw ‎[‎smooth,‎samples=100‎,domain=0:‎‎2‎‎] ‎plot(\x,‎‎‎‎‎‎‎‎{(\x)...‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎});‎‎


But how about functions like x=f(y)? How to plot them? How to specify their domains ‎in terms of "y"‎?

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [smooth,samples=100,domain=0:2] plot(???);
\end{tikzpicture}
\end{document}

-
I'm not sure I understand your question properly. Do you have an expression for f(y) or do you want to determine the inverse of a function f(x)? Or do you just want to change the name of the variable in your plot? Would \draw[domain=0:2,variable=\y] plot({f(\y)},\y); work for you? – benwilfut Mar 27 '13 at 16:05
I agree with benwilfut's comment. I did not read this comment when I posted my answer. I can delete this answer – Alain Matthes Mar 27 '13 at 17:07

\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
\draw[->] (-3,0) -- (4.2,0) node[right] {$x$};
\draw[->] (0,-3) -- (0,4.2) node[above] {$y$};
\draw[scale=0.5,domain=-3:3,smooth,variable=\x,blue] plot ({\x},{\x*\x});
\draw[scale=0.5,domain=-3:3,smooth,variable=\y,red]  plot ({\y*\y},{\y});
\end{tikzpicture}

\end{document}


-
What package are you declaring in the header to do this? – usernumber Nov 17 '14 at 21:24
this is using the tikz package – nosyarg Jan 9 at 16:00

I agree that parametric is the way to go, but one really should be using pgfplots for this kind of thing:

## Code:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmax=9,ymax=9, samples=50]
\end{axis}
\end{tikzpicture}
\end{document}

-

This will work with the gnuplot backend.

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\tikzset{swapaxes/.style = {rotate=90,yscale=-1}}
\draw[red] plot[samples=200,domain=-1:2] function {x**2};
\draw[blue,swapaxes] plot[samples=200,domain=-1:2] function {x**2};
\end{tikzpicture}
\end{document}


-

With PSTricks' swapaxes.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\psset{algebraic}
\begin{document}
\begin{pspicture}[showgrid](-2,-2)(2,2)
\psplot{-1}{1}{x^2}
\psplot[linecolor=red,swapaxes]{-1}{1}{x^2}
\end{pspicture}
\end{document}


Or with PSTricks' \rput and \psscalebox.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\psset{algebraic}
\begin{document}
\begin{pspicture}[showgrid](-2,-2)(2,2)
\psplot{-1}{1.4}{x^2}
\psscalebox{1 -1}{\rput{-90}(0,0){\psplot[linecolor=red]{-1}{1.4}{x^2}}}
\end{pspicture}
\end{document}


Unfortunately, PSTricks has another method with parametric curve as follows.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\psset{algebraic}
\begin{document}
\begin{pspicture}[showgrid](-2,-2)(2,2)
\psplot{-1}{1.4}{x^2}
\psparametricplot[linecolor=red]{-1}{1.4}{t^2|t}
\end{pspicture}
\end{document}

-
I meant TikZ, not PSTricks. – Sisabe Mar 27 '13 at 16:02
@Sisabe: Yes. It is only for those who are using or interested in PSTricks. – kiss my armpit Mar 27 '13 at 16:03
Aren't you missing a reflexion with the \rput version? – cjorssen Mar 27 '13 at 17:25
@cjorssen: done! – kiss my armpit Mar 28 '13 at 0:55