logaritmic axes

Denis Girou Denis.Girou at idris.fr
Thu Apr 9 10:55:15 CEST 1998


-----------------------------------------------------------------------------
This is the PSTricks mailing list, devoted to discussions about computational
graphics in (La)TeX using the PSTricks package from Timothy van Zandt.
For help using this mailing list, see instructions at the end of message.
-----------------------------------------------------------------------------

>>>>> "Paulo.Abreu" == Paulo Tribolet Abreu <paulotex at geocities.com> writes:

    Paulo.Abreu> I've been looking in the docs and in the web pages
    Paulo.Abreu> about how to creat logaritmic axes in pstricks, but
    Paulo.Abreu> I couldn't find anything. Is there a way to automaticaly
    Paulo.Abreu> createm logaritmic tics along an axe?

  Paulo Tribolet Abreu just confirm that my add-on seems usable. In fact,
it is rather simple to just add a "log" parameter accepting the "all|x|y|none"
values as for "ticks" and "labels" parameters. The tick marks are unchanged
and only the labels are modified. They used the macro \pshlabellog
(\psvlabellog) to print them, that can be redefined the same way than
\pshlabel (\psvlabel).

  The only tricky point is to manage negative values. In this case x0 (y0) is
silently forced to x1 (y1). So "\psaxes[log=x]{<->}(-3,0)(3.5,3.5)" will be
the same than "\psaxes[log=x]{<->}(-3,0)(-3,0)(3.5,3.5)".

  Using the same way, I also add yesterday a ticklines=all|x|y|none
parameter, because I found useful sometimes to show the lines corresponding
to the tick marks ("ticksize" doesn't really help here, as it will draw the
correct lines only if the x and y axes are the same, and is not customizable).
The styles of these lines are by default "linestyle=dotted,dotsep=5pt" and can
be redefined using the \TickLinesXStyle (\TickLinesYStyle) macro. We must also
take care in some cases to move a little the labels to don't override them by
these lines.

  Usage is straightforward. Just look at the following examples.

  The next question of Paulo (the one that I expected!) is: "Now, how to draw
the minor tick marks?" Of course, minor tick marks are not supported at the
moment. It must be relatively easy to add them for "normal" axes, but more
difficult for logarithmic ones. The main question here is: where to put the
frontier between what must be available and what must be kept away if we don't 
want to just reimplement all gnuplot features (for instance)... The couple
\psaxes + \psplot (and his friends \parametricplot, \dataplot, etc.) is really 
powerful. But we can dream to many other features... So, I must think a little 
about minor tick marks...

D.G.

\documentclass{article}

\usepackage{pstcol}
\usepackage{pst-plot}

\makeatletter

% D.G. - April 1998

% Define "log" parameter (log=all|x|y|none with default=none)
\def\psset at log#1{%
\pst at expandafter\psset@@ticks{#1}\@nil\psk at log%
% X axis labels (\psk at log = 0 or 1)
\ifnum\psk at log<\tw@\let\pshlabel\pshlabellog\fi
% Y axis labels (\psk at log = 0 or 2)
\ifodd\psk at log\else\let\psvlabel\psvlabellog\fi}
\psset at log{none}

% Default formats for log labels
\def\pshlabellog#1{$10^{#1}$}
\def\psvlabellog#1{$10^{#1}$}

% Define "ticklines" parameter (ticklines=all|x|y|none with default=none)
\def\psset at ticklines#1{\pst at expandafter\psset@@ticks{#1}\@nil\psk at ticklines}
\psset at ticklines{none}

% The parameter styles for the tick lines
% (default=arrows=-,linestyle=dotted,dotsep=5pt)
\def\TickLinesXStyle{}
\def\TickLinesYStyle{}

% Modified \psaxes at iv macro for log axis
\def\psaxes at iv(#1,#2)(#3,#4)(#5,#6){%
  \setbox\pst at hbox=\hbox\bgroup
    \use at par
    \pssetxlength\pst at dimg{#1}% o-x
    \pssetylength\pst at dimh{#2}% o-y
    \pssetxlength\pst at dima{#3}% bl-x
    \pssetylength\pst at dimb{#4}% bl-y
    \pssetxlength\pst at dimc{#5}% ur-x
    \pssetylength\pst at dimd{#6}% ur-y
% D.G. modification begin - Apr.  6, 1998
    % If minimum values are negative in log mode, we modify Ox
    % (respectively Oy) if this was not done by the user
    % X axis labels (\psk at log = 0 or 1)
    \ifnum\psk at log<\tw@
      \ifdim\psk at Ox pt=\z@
        \ifdim#3pt<\z@
          \pssetxlength\pst at dimg{#3}% o-x
          \psset at Ox{#3}%
        \fi
      \fi
    \fi
    % Y axis labels (\psk at log = 0 or 2)
    \ifodd\psk at log
    \else
      \ifdim#4pt<\z@
        \ifdim\psk at Oy pt=\z@
          \pssetylength\pst at dimh{#4}% o-y
          \psset at Oy{#4}%
        \fi
      \fi
    \fi
% D.G. modification end
% Whole thing will be translated to origin:
    \advance\pst at dima-\pst at dimg % Dist. from bl-x to o-x
    \advance\pst at dimb-\pst at dimh % Dist. from bl-y to o-y
    \advance\pst at dimc-\pst at dimg % Dist. from ur-x to o-x
    \advance\pst at dimd-\pst at dimh % Dist. from ur-y to o-y
% Make lines/arrows or frame:
    \@nameuse{psxs@\psk at axesstyle}%
% "\pslabelsep" should be from the edge of the axis.
    \advance\pslabelsep.5\pslinewidth
% Now the ticks and labels. Start by checking for "\multido".
% !!Need to fix this so that does nothing when there are 0 ticks.!!
    \begingroup
      \ifdim\pst at dimb=\z@\else\showoriginfalse\fi
      \ifnum\psk at dx=\z@
        \pst at dimg=\psk at Dx\psxunit
        \edef\psk at dx{\number\pst at dimg}%
      \fi
      \ifnum\psk at ticks<\tw@
        \ifnum\psk at tickstyle>\z@\else
          \advance\pslabelsep\psk at ticksize\p@
        \fi
      \fi
% D.G. modification begin - Apr.  8, 1998 - For vertical ticklines
%      \pst at hlabels\pst at dimc\psk at arrowB
%      \pst at hlabels\pst at dima\psk at arrowA
      \pst at hlabels{\pst at dimc}{\psk at arrowB}{#4}{#6}
      \pst at hlabels{\pst at dima}{\psk at arrowA}{#4}{#6}
% D.G. modification end
    \endgroup
    \begingroup
      \ifdim\pst at dima=\z@\else\showoriginfalse\fi
      \ifnum\psk at dy=\z@
         \pst at dimg=\psk at Dy\psyunit
         \edef\psk at dy{\number\pst at dimg}%
      \fi
      \ifodd\psk at ticks\else
        \ifnum\psk at tickstyle>\z@\else
          \advance\pslabelsep\psk at ticksize\p@
        \fi
      \fi
% D.G. modification begin - Apr.  8, 1998 - For horizontal ticklines
%      \pst at vlabels\pst at dimd\psk at arrowB
%      \pst at vlabels\pst at dimb\psk at arrowA
      \pst at vlabels{\pst at dimd}{\psk at arrowB}{#3}{#5}
      \pst at vlabels{\pst at dimb}{\psk at arrowA}{#3}{#5}
% D.G. modification end
    \endgroup
% Now close "\pst at hbox" (which is 0-dimensional), and put it at the origin.
  \egroup
  \pssetxlength\pst at dimg{#1}%
  \pssetylength\pst at dimh{#2}%
  \leavevmode\psput at cartesian\pst at hbox
  \ignorespaces}

% The origin is never the only label.
% D.G. modification begin - Apr.  8, 1998
% Modified \pst at hlabels macro for tick lines
%\def\pst at hlabels#1#2{%
\def\pst at hlabels#1#2#3#4{%
% D.G. modification end
  \ifdim#1=\z@\else
    \ifx#2\empty\else
      \advance#1\ifdim#1>\z at -\fi7\pslinewidth
    \fi
    \pst at cnta=#1\relax                % Distance (in sp) to end.
    \divide\pst at cnta\psk at dx\relax     % Number of ticks/labels
    \ifnum\pst at cnta=\z@\else
      \pst at dimb=\psk at dx sp            % Space between ticks.
      \ifnum\psk at ticks<\tw@
        \pst at ticks{0}{\pst at number\pst at dimb}{\the\pst at cnta}{\pst at dimd}%
      \fi
      \ifnum\psk at labels<\tw@ \pst@@hlabels\fi
      \showoriginfalse
% D.G. modification begin - Apr.  8, 1998 - For vertical ticklines
      \ifnum\psk at ticklines<\tw@
        \pst at cntb=\pst at cnta
        \ifnum\pst at cnta<\z@
          \multiply\pst at cntb\m at ne
        \fi
        \multips(\ifnum\pst at cnta<\z at -\fi\pst at dimb,0)%
                (\ifnum\pst at cnta<\z at -\fi\pst at dimb,0){\pst at cntb}{%
          \psset{arrows=-,linestyle=dotted,dotsep=5pt}
          \ifx\TickLinesYStyle\empty\else\TickLinesYStyle\fi
          \psline(0,#3)(0,#4)}
      \fi
% D.G. modification end
    \fi
  \fi}

% D.G. modification begin - Apr.  8, 1998
% Modified \pst at vlabels macro for tick lines
%\def\pst at vlabels#1#2{%
\def\pst at vlabels#1#2#3#4{%
% D.G. modification end
  \ifdim#1=\z@\else
    \ifx#2\empty\else
      \advance#1\ifdim#1>\z at -\fi7\pslinewidth
    \fi
    \pst at cnta=#1\relax                % Distance (in sp) to end.
    \divide\pst at cnta\psk at dy\relax     % Number of ticks/labels
    \ifnum\pst at cnta=\z@\else
      \pst at dima=\psk at dy sp            % Space between ticks.
      \ifodd\psk at ticks\else
        \pst at ticks{90}{\pst at number\pst at dima}{\the\pst at cnta}{-\pst at dimc}%
      \fi
      \ifodd\psk at labels\else\pst@@vlabels\fi
      \showoriginfalse
% D.G. modification begin - Apr.  8, 1998 - For horizontal ticklines
      \ifodd\psk at ticklines
      \else
        \pst at cntb=\pst at cnta
        \ifnum\pst at cnta<\z@
          \multiply\pst at cntb\m at ne
        \fi
        \multips(0,\ifnum\pst at cnta<\z at -\fi\pst at dima)%
                (0,\ifnum\pst at cnta<\z at -\fi\pst at dima){\pst at cntb}{%
          \psset{arrows=-,linestyle=dotted,dotsep=5pt}
          \ifx\TickLinesXStyle\empty\else\TickLinesXStyle\fi
          \psline(#3,0)(#4,0)}
      \fi
% D.G. modification end
    \fi
  \fi}

\makeatother

\textwidth=18cm
\textheight=25cm
\topmargin=-2cm
\oddsidemargin=-2cm
\evensidemargin=-2cm

\pagestyle{empty}

\begin{document}

% "log" parameter examples
% ------------------------

\begin{figure}[!htbp]
  \centering
  \begin{pspicture}(-0.5,-4)(6.5,1.5)
    \psplot{0.001}{6}{x log}      % log(x)
    \psaxes[log=y]{<->}(0,-3)(6.5,1.5)
  \end{pspicture}
  \caption{Demonstration of log axes usage: (\texttt{log=y})}
\end{figure}

\begin{figure}[!htbp]
  \centering
  \begin{pspicture}(-3.5,-1)(3.5,3.5)
    % Warning! As you can see comparing to the next graphic, (x0,y0) will be
    % forced to (-3,0) because negative values have no meaning in log mode
    \psaxes[log=x]{<->}(-3,0)(3.5,3.5)
  \end{pspicture}
  \caption{Demonstration of log axes usage: (\texttt{log=x})}
\end{figure}

\begin{figure}[!htbp]
  \centering
  \begin{pspicture}(-3.5,-1)(3.5,3.5)
    \psaxes[log=x]{<->}(-3,0)(-3,0)(3.5,3.5)
  \end{pspicture}
  \caption{Demonstration of log axes usage: (\texttt{log=x})}
\end{figure}

\begin{figure}[!htbp]
  \centering
  \begin{pspicture}(-3.5,-1)(3.5,2.5)
    \psaxes{<->}(0,0)(-3.5,0)(3.5,2.5)
  \end{pspicture}
  \caption{Demonstration of log axes usage: (\texttt{log=none})}
\end{figure}

\begin{figure}[!htbp]
  \centering
  % Redefinition of the format for horizontal log labels
  \renewcommand{\pshlabellog}[1]{\color{blue}$\mathit{10^{#1}}$}
  \begin{pspicture}(-6.5,-1)(6.75,3.5)
    \psaxes[xunit=1.5,log=x]{<->}(-4,0)(4.5,3.5)
  \end{pspicture}
  \caption{Demonstration of log axes usage: (\texttt{log=x})}
\end{figure}

\begin{figure}[!htbp]
  \centering
  % Redefinition of the formats for log labels
  \renewcommand{\pshlabellog}[1]{\color{green}\small$\mathit{10^{#1}}$}
  \renewcommand{\psvlabellog}[1]{\color{red}\small$\mathbf{10^{#1}}$}
  \begin{pspicture}(-3.5,-4)(3.5,3.5)
    \psaxes[log=all]{<->}(-3,-3)(3.5,3.5)
  \end{pspicture}
  \caption{Demonstration of log axes usage: (\texttt{log=all})}
\end{figure}

\begin{figure}[!htbp]
  \centering
  \begin{pspicture}(-0.5,1)(6.5,4.5)
    \psaxes[Oy=2]{<->}(0,2)(0,2)(6.5,4.5)
  \end{pspicture}
  \caption{Demonstration of log axes usage: (\texttt{log=none})}
\end{figure}

\begin{figure}[!htbp]
  \centering
  \begin{pspicture}(-0.5,1)(6.5,4.5)
    \psaxes[log=y,Oy=2]{<->}(0,2)(0,2)(6.5,4.5)
  \end{pspicture}
  \caption{Demonstration of log axes usage: (\texttt{log=y})}
\end{figure}

\begin{figure}[!htbp]
  \centering
  \begin{pspicture}(-0.5,-1)(6.5,4.5)
    \psplot{0}{6}{x x cos add log}                       % x   + cox(x)
    \psplot[linecolor=red]{0}{6}{x 3 exp x cos add log}  % x^3 + cos(x)
    \psplot[linecolor=cyan]{0}{6}{x 5 exp x cos add log} % x^5 + cos(x)
    \psaxes[log=y]{<->}(6.5,4.5)
  \end{pspicture}
  \caption{Demonstration of log axes usage: (\texttt{log=y})}
\end{figure}

\begin{figure}[!htbp]
  \centering
  \begin{pspicture}(-0.5,-2)(6.5,4.5)
    \psplot{0}{6}{x x cos add log}                       % x   + cox(x)
    \psplot[linecolor=red]{0}{6}{x 3 exp x cos add log}  % x^3 + cos(x)
    \psplot[linecolor=cyan]{0}{6}{x 5 exp x cos add log} % x^5 + cos(x)
    \psaxes[log=y]{<->}(0,-1)(0,-1)(6.5,4.5)
  \end{pspicture}
  \caption{Demonstration of log axes usage: (\texttt{log=y})}
\end{figure}

\begin{figure}[!htbp]
  \centering
  \begin{pspicture}(2.5,1)(6.5,4.5)
    \psaxes[Ox=3,Oy=2]{<->}(3,2)(3,2)(6.5,4.5)
  \end{pspicture}
  \caption{Demonstration of log axes usage: (\texttt{log=none})}
\end{figure}

\begin{figure}[!htbp]
  \centering
  \begin{pspicture}(2.5,1)(6.5,4.5)
    \psplot[linecolor=cyan]{3}{6}{x 5 exp x cos add log} % x^5 + cos(x)
    \psaxes[log=y,Ox=3,Oy=2]{<->}(3,2)(3,2)(6.5,4.5)
  \end{pspicture}
  \caption{Demonstration of log axes usage: (\texttt{log=y})}
\end{figure}

% "ticklines" parameter examples
% ------------------------------

\begin{figure}[!htbp]
  \centering
  \begin{pspicture}(-3.5,-3.5)(3.5,2.5)
    \psaxes{<->}(0,0)(-3.5,-2.5)(3.5,2.5)
  \end{pspicture}
  \caption{Demonstration of ticklines usage: (\texttt{ticklines=none})}
\end{figure}

\begin{figure}[!htbp]
  \centering
  \renewcommand{\pshlabel}[1]{\rput(0.3,0){$#1$}}
  \begin{pspicture}(-3.5,-3.5)(3.5,2.5)
    \psaxes[ticklines=x]{<->}(0,0)(-3.5,-2.5)(3.5,2.5)
  \end{pspicture}
  \caption{Demonstration of ticklines usage: (\texttt{ticklines=x})}
\end{figure}

\begin{figure}[!htbp]
  \centering
  \renewcommand{\TickLinesXStyle}{\psset{linestyle=dashed}}
  \renewcommand{\psvlabel}[1]{\mbox{\rput[r](0,-0.2){$#1$}}}
  \begin{pspicture}(-3.5,-3.5)(3.5,2.5)
    \psaxes[ticklines=y]{<->}(0,0)(-3.5,-2.5)(3.5,2.5)
  \end{pspicture}
  \caption{Demonstration of ticklines usage: (\texttt{ticklines=y})}
\end{figure}

\begin{figure}[!htbp]
  \centering
  \renewcommand{\TickLinesXStyle}{\psset{linecolor=red}}
  \renewcommand{\TickLinesYStyle}{\psset{linecolor=cyan}}
  \renewcommand{\pshlabel}[1]{\rput(0.3,0){$#1$}}
  \renewcommand{\psvlabel}[1]{\mbox{\rput[r](0,-0.2){$#1$}}}
  \begin{pspicture}(-3.5,-3.5)(3.5,2.5)
    \psaxes[ticklines=all]{<->}(0,0)(-3.5,-2.5)(3.5,2.5)
  \end{pspicture}
  \caption{Demonstration of ticklines usage: (\texttt{ticklines=all})}
\end{figure}

\begin{figure}[!htbp]
  \centering
  \begin{pspicture}(-0.5,-2)(6,4)
    \psplot{0}{6}{x x cos add log}                       % x   + cox(x)
    \psplot[linecolor=red]{0}{6}{x 3 exp x cos add log}  % x^3 + cos(x)
    \psplot[linecolor=cyan]{0}{6}{x 5 exp x cos add log} % x^5 + cos(x)
    \psaxes[axesstyle=frame,log=y,ticklines=y]{<->}(0,-1)(6,4)
  \end{pspicture}
  \caption{Demonstration of log axes and ticklines usage:
           (\texttt{log=y,ticklines=y})}
\end{figure}

\end{document}

-----------------------------------------------------------------------------
The list interface (subscription, information, access to the archives) is on:
http://www.tug.org/cgi-bin/lwgate/pstricks
Otherway to unsubscribe, send mail to pstricks-request at mail.tug.org
with a blank subject and in body the line unsubscribe <email-address>
-----------------------------------------------------------------------------



More information about the PSTricks mailing list