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}
Mensch
  • 65,388
nemarona
  • 959
  • 7
  • 17

5 Answers5

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}

Peter Grill
  • 223,288
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

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}
  • 2
    Rotations are not inverse functions! E.g. y=x – strpeter Jan 22 '14 at 21:53
  • @strpeter: Yes of course but nobody knows if you just see the image. – kiss my armpit Mar 20 '14 at 03: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. – kiss my armpit Mar 20 '14 at 22:52
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}

I. Cho
  • 2,985
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

Frank Zafka
  • 1,189