# How can I use \psgraph with polar axes?

## With pspicture

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\psset{axesstyle=polar,plotpoints=100}
\begin{document}
\begin{pspicture}(-4,-4)(4,4)
\psaxes[axesstyle=polar,linecolor=gray](3,360)
\psplot[algebraic,polarplot,linecolor=red]{0}{TwoPi}{sin(3*x)+2}
\end{pspicture}
\end{document}


## With psgraph

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\psset{axesstyle=polar,plotpoints=100}
\begin{document}
\begin{psgraph}[linecolor=gray](3,360){4cm}{4cm}
\psplot[algebraic,polarplot,linecolor=red]{0}{TwoPi}{sin(3*x)+2}
\end{psgraph}
\end{document}


How can I use psgraph with polar axes?

-
How come you want to use psgraph? Is it so that you can specify the width and height? I'm interested because I have another solution (if that's the case)... –  cmhughes Jul 14 '14 at 20:14
Because I want to make every graph use psgraph for the sake of consistency. I reserve pspicture is for drawing non-graph. –  kiss my armpit Jul 14 '14 at 20:17
what advantages does psgraph offer over pspicture? –  cmhughes Jul 14 '14 at 20:31
@cmhughes: With psgraph, we can specify the size of graph in advance and the vertical and horizontal units are automatically adjusted. It is useful for the case in which we have a dimension constraint in our articles, books, or any documents. For example, if I want to put the graph in the marginpar, it is much better if we specify the dimension of the graph (via psgraph) to be equal to the width of the marginpar such that we don't need to scale the graph with width=\linewidth (of includegraphics) which badly also affects the size of labels attached to the graph. –  kiss my armpit Jul 15 '14 at 3:29
I have a solution that can fix this for psgraph - I'll post it as an answer at some point (too busy at the moment).... –  cmhughes Jul 15 '14 at 21:30

It makes no sense to use \psgraph here: it is a simple scaling of the graph.

\documentclass{article}
\usepackage{pst-plot}
\begin{document}

\frame{\psscaleboxto(4cm,4cm){%
\psset{axesstyle=polar,plotpoints=100,plotstyle=curve}
\begin{pspicture}(-4,-4)(4,4)
\psaxes[axesstyle=polar,linecolor=gray](3,360)
\psplot[algebraic,polarplot,linewidth=1.5pt,linecolor=red]{0}{TwoPi}{sin(3*x)+2}
\end{pspicture}}}
\frame{\psscaleboxto(4cm,4cm){%
\psset{axesstyle=polar,plotpoints=100,plotstyle=curve}
\begin{pspicture}(-0.2,-0.5)(4,4)
\psaxes[axesstyle=polar,linecolor=gray](3,90)
\psplot[algebraic,polarplot,linewidth=1.5pt,linecolor=blue]{0}{Pi2}{sin(3*x)+2}
\end{pspicture}}}

\rule{4cm}{1pt}

\end{document}


And just for fun:

\documentclass{article}
\usepackage{pst-plot}
\begin{document}

\psset{axesstyle=polar,plotpoints=10000,plotstyle=curve}
\begin{pspicture}(-4,-4)(4,4)
\psaxes[axesstyle=polar,linecolor=gray](3,360)
\psplot[algebraic,polarplot,linewidth=1.5pt,fillstyle=eofill,fillcolor=blue!60,
opacity=0.5,linecolor=red]{0}{20 Pi mul}{sin(3*x)+2*cos(x/2)}
\end{pspicture}

\end{document}


-
The second axesstyle=polar can be removed to save more keystrokes. –  kiss my armpit Jul 14 '14 at 19:41
I love keystrokes ... –  Herbert Jul 14 '14 at 19:45
The axis is in degrees, but the function domain is specified in radians. Is it possible to specify the function domain in degrees? –  Jake Jul 14 '14 at 20:13
sure, it is ... it is the default –  Herbert Jul 14 '14 at 20:15
@Herbert: If I use \psplot[algebraic,polarplot,linewidth=1.5pt,linecolor=red]{0}{360}{sin(3*x)+2}‌​, I don't get the same plot. –  Jake Jul 14 '14 at 20:17

Just for fun with PGFPlots:

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{polar}
\pgfplotsset{compat=1.10}

\begin{document}
\begin{tikzpicture}
\begin{polaraxis}[enlargelimits=false, xticklabel=$\pgfmathprintnumber{\tick}^\circ$]
\addplot [thick, red, domain=0:360, samples=100] {sin(3*x)+2};
\end{polaraxis}
\end{tikzpicture}
\end{document}


If you want to specify the function and domain in terms of radians, set data cs=polarrad. this can be useful if you want to plot a data file that uses radians, for instance.

If you want to plot a function with data cs=polarrad, note that the math parser will still expect the input to trigonometric functions to be in degrees, so you'll have to use sin(deg(...)) instead of sin(...), for example.

-
+1 Unfortunately, tikz has no tikzgraph which is the equivalent of psgraph. –  kiss my armpit Jul 14 '14 at 19:02
@Pleasedon'ttouch: Doesn't psgraph just scale the coordinates as needed? PGFPlots does this. –  Jake Jul 14 '14 at 19:09
@Jake: the step width is counted in degrees. Are radians also possible? –  Herbert Jul 14 '14 at 19:31
Can you make the angle in degrees with \circ? Without \circ ones might think the angle in radian. –  kiss my armpit Jul 14 '14 at 19:49
@Pleasedon'ttouch: Sure. I've edited my answer. –  Jake Jul 14 '14 at 19:52

With psgraph, we can specify the size of graph in advance and the vertical and horizontal units are automatically adjusted.

The same thing troubled me when I first started using pstricks - we can specify xunit and yunit using psset, but we simply have to live with whatever the width and height of the figure turn out to be. As far as I could tell, there was no way to specify the width and/or height and then calculate xunit and yunit.

Of course, a little bit of mathematics can be invoked and, given a width and values of xmin and xmax, we can calculate xunit; similarly for a height, with given values of ymin and ymax we can compute yunit.

The code below defines a setwindow command that is invoked in the following way:

\setwindow[optional height ratio]{xmin}{xmax}{ymin}{ymax}{\figurewidth}


For example, it can be used as

\setlength{\figurewidth}{.5\textwidth}
\setwindow{0}{-.5}{3}{3}{\figurewidth}


which will set the pspicture environment to have width and height of half of the text width, with xmin=0, ymin=-.5, xmax=3, and ymax=3.

The optional argument can be used to scale the height with respect to the width. For example, using

\setwindow[.5]{0}{-.5}{3}{3}{\figurewidth}


will set the height of the pspicture to be half of the width. Using

\setwindow[.25]{0}{-.5}{3}{3}{\figurewidth}


will set the height to be a quarter of the width.

Here's a complete example to play with - I hope it's useful!

% arara: latex
% arara: dvips
% arara: ps2pdf
% !arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage{pst-plot}
\usepackage[showframe=true]{geometry}

\newlength{\figurewidth}
% setwindow command: takes 6 arguments (1st is optional)
%       [1]. aspect ratio (height = aspect ratio * width) default=1
%         2. xmin (number)
%         3. ymin (number)
%         4. xmax (number)
%         5. ymax (number)
%         6. figurewidth (length)
\makeatletter
\newcommand{\setwindow}[6][1]{\def\xmin{#2}%
\def\ymin{#3}%
\def\xmax{#4}%
\def\ymax{#5}%
% set up xunit
\pstFPsub\viewingwidth{#4}{#2}%
\pstFPdiv\result{\strip@pt#6}{\viewingwidth}%
\psset{xunit=\result pt}
% set up yunit
\pstFPsub\viewingheight{#5}{#3}%
\pstFPdiv\result{\strip@pt#6}{\viewingheight}%
% set up aspect ratio
\pstFPmul\result{\result}{#1}%
\psset{yunit=\result pt}%
}

\begin{document}

\setlength{\figurewidth}{.5\textwidth}
\psset{axesstyle=polar,plotpoints=100,plotstyle=curve}
\setwindow[.5]{0}{-.5}{3}{3}{\figurewidth}
\noindent\begin{pspicture}(\xmin,\ymin)(\xmax,\ymax)
\psgrid
\psaxes[axesstyle=polar,linecolor=gray](3,90)
\psplot[algebraic,polarplot,linewidth=1.5pt,linecolor=blue]{0}{Pi2}{sin(3*x)+2}
\end{pspicture}

\end{document}


PS: the arara directives are included for your convenience :)

-
But you are using pspicture rather than psgraph. –  kiss my armpit Jul 22 '14 at 2:16
@Pleasedon'ttouch that was precisely the point :) –  cmhughes Jul 22 '14 at 16:39
Oh my ghost. What you did is almost identical to what I already did. I will show it shortly. –  kiss my armpit Jul 22 '14 at 16:41