17

pgfplots is not quite comfortable plotting sqrt(x) near x=0. The graphic can be improved by setting samples=2000 (not less), but this seems overkill to me.

Is there a way to plot x as a function of y, while keeping the x axis horizontal and the y axis vertical? This would solve the issue of plotting sqrt(x) by simply replacing it with x=y^2.

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
 \begin{axis}
  \addplot[] {sqrt(x)};
  \addplot[] {-sqrt(x)};
 \end{axis}
\end{tikzpicture}

\end{document}

5 Answers 5

23

You could use a parametrized plot:

enter image description here

Code:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
 \begin{axis}
  \addplot[red, ultra thick] (x*x,x);
 \end{axis}
\end{tikzpicture}

\end{document}
0
8

Just for a comparison purpose how PSTricks does it with swapaxes option.

enter image description here

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\begin{document}

\begin{pspicture}(-3,-3)(5,5)
\psaxes[linecolor=gray]{->}(0,0)(-3,-3)(5,5)
\parabola[linecolor=blue](2,4)(0,0)
\parabola[linecolor=red,swapaxes](2,4)(0,0)
\end{pspicture}

\end{document}

Note:

\parabola[linecolor=red,swapaxes](2,4)(0,0)

passes through the point (2,4) with a critical point (0,0).

4
  • That's exactly what I was looking for. Is there a way to do the same in pgfplots?
    – nemarona
    Sep 16, 2012 at 6:58
  • @nemarona: If such an option does not exist in pgf/tikz, then you might have to use a transformation (rotation is enough). Sep 16, 2012 at 8:36
  • "swap axes" in pgfplots means to use a parametric plot as in the answer of Peter Grill - and exchanging the X and Y expressions manually. Sep 16, 2012 at 16:59
  • @ChristianFeuersänger: Thank you for the useful information. Sep 16, 2012 at 17:01
4

Use rotation. Here is the minimal code:

enter image description here

\documentclass[tikz,border=12pt]{standalone}

\begin{document}
\begin{tikzpicture}
\begin{scope}[rotate=-90]
    \draw[domain=-2:2] plot (\x,\x*\x);
\end{scope}
\end{tikzpicture}
\end{document}
3
  • 2
    Rotations are not inverse functions! E.g. y=x
    – strpeter
    Jan 22, 2014 at 21:53
  • @strpeter: Yes of course but nobody knows if you just see the image. Mar 20, 2014 at 3:04
  • 1
    @strpeter: The problem is that the question is misleading so I just followed what the questioner wanted to get no matter his incorrect usage of the term "inverse function". I understand that inverse function must have a single value for each input. Mar 20, 2014 at 22:52
1

I don't quite understand the mathematics side of it, but I often use R to produce my graphics using tikzDevice.

R code (produces tex then graphic):

library(tikzDevice)
tikz('normal.tex', standAlone = TRUE, width=5, height=5)
x <- seq (-50, 50, length = 50)
xx <-x^2
plot(xx, type="l")
dev.off()

enter image description here

1
  • One step more is needed to rotate the graph 90 degree clockwise. Sep 16, 2012 at 9:08
1

Using tzplot:

enter image description here

\documentclass{standalone} 

\usepackage{tzplot}

\begin{document}

\begin{tikzpicture}
\tzaxes(-1,-3)(5,3){$x$}{$y$}
\tzfnofy[blue,thick]{(\y)^2}[-2:2]{$f(y)=y^2$}[a] % function of y
\end{tikzpicture}

\end{document}

You must log in to answer this question.

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