3

I'm trying to rotate a simple Gaussian, but when I try to rotate the graph, it seems clear that pgfplots is simply cropping the graph to match the boundaries specified:

enter image description here

The above picture shows two graphs, one is blue and isn't rotated. This graph has the correct boundaries, x is between 0 and 5.

The other graph in orange is rotated. The graph is then cropped after rotating. I wish to reverse this order, so that I can have the shape of the blue graph, rotated like the orange graph.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}


\pgfmathdeclarefunction{gauss}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\begin{document}
\begin{tikzpicture}[xscale=1,>=latex,
    declare function={
        normal(\m,\s)=1/(2*\s*sqrt(pi))*exp(-(x-\m)^2/(2*\s^2));
    }
]
\begin{axis}[
    axis lines=none,
    samples=30,
    xmin=0,xmax=5,
    domain=-5:5,
    area plot/.style={
        fill opacity=0.75,
        draw=none,
        fill=orange,
        mark=none,
        smooth
    }
]
\addplot [area plot,rotate=25,yshift=-50,xshift=50]  {normal(0,1)};
\addplot [area plot]  {normal(0,1)};
\end{axis}
\end{tikzpicture}

\end{document}
4

A tikzpicture is still a tikzpicture. So you can apply transformations to it.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfmathdeclarefunction{gauss}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\begin{document}
\begin{tikzpicture}[>=latex,rotate around={50:(0,0)},% <-- Here
    declare function={normal(\m,\s)=1/(2*\s*sqrt(pi))*exp(-(x-\m)^2/(2*\s^2));}
]
\begin{axis}[
    axis lines=none,
    samples=30,
    xmin=0,xmax=5,
    domain=-5:5,
    area plot/.style={
        fill opacity=0.75,
        draw=none,
        fill=orange,
        mark=none,
        smooth
    }
]
\addplot [area plot]  {normal(0,1)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • For some reason I really like this! – 1010011010 Nov 6 '14 at 11:57
2

I don't know the relationship between TiKZ's plot coordinates and pgfplots so the results are not the same. But you don't need to use pgfplots to draw something like your example, just a fill with plot is enough and don't have to worry with clipped areas.

\documentclass[border=3mm,tikz]{standalone}

\begin{document}
\begin{tikzpicture}[declare function={%
    gauss(\x)=1/(sqrt(2*pi))*exp(-(\x)^2/2);},]

\fill[color=orange, fill opacity=.75] (0,0) --plot[ domain=0:5] (\x,{gauss(\x)}) -- cycle;

\fill[color=blue, fill opacity=.75,yshift=5mm, xshift=5mm, rotate=25 ] (0,0) -- plot[ domain=0:5] (\x,{gauss(\x)}) -- cycle;

\fill[color=red, fill opacity=.75,yshift=5, xshift=5mm, rotate=50] (0,0) -- plot[ domain=0:5] (\x,{gauss(\x)}) -- cycle;
\end{tikzpicture}
\end{document}

enter image description here

1

One impractical way to do this is by embedding the tikzpicture in a rotatebox, to then make that tikzpicture a node inside another tikzpicture:

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}


\pgfmathdeclarefunction{gauss}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\begin{document}
\begin{tikzpicture}

\node at (0,0){\rotatebox{45}{\begin{tikzpicture}[xscale=1,>=latex,
    declare function={
        normal(\m,\s)=1/(2*\s*sqrt(pi))*exp(-(x-\m)^2/(2*\s^2));
    }
]
\begin{axis}[
    axis lines=none,
    samples=30,
    xmin=0,xmax=5,
    domain=-5:5,
    area plot/.style={
        fill opacity=0.75,
        draw=none,
        fill=orange,
        mark=none,
        smooth
    }
]
%\addplot [area plot,rotate=25,yshift=-50,xshift=50]  {normal(0,1)};
\addplot [area plot,fill=blue]  {normal(0,1)};
\end{axis}
\end{tikzpicture}}};

\end{tikzpicture}

\end{document}
  • 1
    Almost any answer will beat this one since embedding tikzpictures in nodes is one of the seven deadly sins... – 1010011010 Nov 6 '14 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.