PGF has the \pgfmathprintnumber
macro that allows you to print and format numbers, including rounding. To round to two decimal digits, you would use \pgfmathprintnumber[precision=2]{\pgfmathresult}
, for example.
The unit vectors are stored in \pgf@xx
(for the x-component of the x unit vector) and \pgf@yy
(the y-component of the y unit vector). So your function could be shortened to
\makeatletter
\newcommand\xcoord[2][center]{{%
\pgfpointanchor{#2}{#1}%
\pgfmathparse{\pgf@x/\pgf@xx}%
\pgfmathprintnumber{\pgfmathresult}%
}}
\newcommand\ycoord[2][center]{{%
\pgfpointanchor{#2}{#1}%
\pgfmathparse{\pgf@y/\pgf@yy}%
\pgfmathprintnumber{\pgfmathresult}%
}}
\makeatother
You can use the data cs
key to set the coordinate system in which the data is interpreted. This works perfectly fine for 3D plots as well, supplying a third coordinate automatically acts as a cylindrical projection. See also section 4.24 Transforming Coordinate Systems of the pgfplots
manual (v1.10).
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot3+[data cs=polarrad,domain=0:2*pi] (\x,1,2*\x);
\end{axis}
\end{tikzpicture}
\end{document}
Update: Added a faked 3D polaraxis
, this could definitely use some improvement, but might get you started. A better solution would apply the style of the "polar axis" somewhat nicer, and currently the labels are clipped, also the radius is fixed, that should probably be somehow dependent on the plotted data, but I don't have time to delve into this that deep at the moment.
I also changed the data cs
to polar
because I didn't get the foreach \thet
to work with fractions of pi
.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{polar}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=middle,
axis y line=middle,
axis z line=middle,
ytick=\empty,
data cs=polar,
]
\foreach \r in {0,0.25,...,1.25} {
\addplot+[domain=0:360,mark=none,black,samples=200, smooth] (\x, \r);
}
\foreach \thet in {0,30,...,330} {
% some trickery to get the label expanded
\edef\doplot{\noexpand\addplot+[domain=0:1.3,mark=none,black] (\thet, \noexpand\x) node[pos=1.2] {\thet};}
\doplot
}
\addplot3+[domain=0:360] (\x,1,2*\x);
\end{axis}
\end{tikzpicture}
\end{document}
Best Answer
You could use
yscale=-1
: