116

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}
2
  • 4
    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? Mar 27, 2013 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 Mar 27, 2013 at 17:07

5 Answers 5

159
\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}

Graphs of the binary relations y = x^2 and x = y^2

1
  • How can I include it in the main document? Oct 4, 2020 at 4:56
50

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

enter image description here

Code:

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

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmax=9,ymax=9, samples=50]
  \addplot[blue, ultra thick] (x,x*x);
  \addplot[red,  ultra thick] (x*x,x);
\end{axis}
\end{tikzpicture}
\end{document}
16

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}

enter image description here

2
  • 2
    What does "This will work with the gnuplot backend" mean? Where is this "backend" thing that I can put gnuplot in?
    – user207442
    Aug 29, 2018 at 14:00
  • @user207442 You just have to have gnuplot installed with write18 enabled and it should work out of the box.
    – cjorssen
    Sep 7, 2018 at 13:41
10

With PSTricks' swapaxes.

enter image description here

\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}
2
  • 1
    I meant TikZ, not PSTricks.
    – Sisabe
    Mar 27, 2013 at 16:02
  • 4
    @Sisabe: Yes. It is only for those who are using or interested in PSTricks. Mar 27, 2013 at 16:03
1

Using tzplot:

enter image description here

\documentclass{standalone} 

\usepackage{tzplot}

\begin{document}

\begin{tikzpicture}
\tzaxes(-2,-2)(5,5){$x$}{$y$}
\tzfn[blue,thick]{(\x)^2}[-1:2]{$f(x)$}[ar]
\tzfn'[red,thick]{(\x)^2}[-1:2]{$f^{-1}(x)$}[ar] % inverse
\end{tikzpicture}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .