2

Sorry if it's a FAQ, probably looking for the wrong keywords but couldn't find anything.

I have a macro that takes a length parameter to draw a shape. I'd like to call this macro inside a pgfplots axis. And I'd like to have the length parameter in axis coordinates units.

Now, I know how to specify absolute coordinates with (axis cs: x,y) but is there a way to get the axis unit length?

Here's the code

\documentclass[11pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{pgfplots}

% draw a circle of radius #1 with randomness #2
\newcommand\irregularcircle[2]{%
  \pgfextra {%
    \pgfmathsetseed{42}%
    \pgfmathsetmacro\len{(#1)+rand*(#2)}}
  +(0:\len pt)
  \foreach \a in {20,40,...,340}{
    \pgfextra {\pgfmathsetmacro\len{(#1)+rand*(#2)}}
    -- +(\a:\len pt)
  } -- cycle
}

\begin{document}
\begin{tikzpicture}[auto]
  \begin{axis}[%
    axis x line=center,
    axis y line=center,
    xmin=-8, xmax=8,
    ymin=-8, ymax=8,
    ]
    \draw[rounded corners=2mm, fill=black, opacity=0.2] (axis cs:0,0) \irregularcircle{2cm}{2mm};
  \end{axis}
\end{tikzpicture}
\end{document}

I'd like to give the circle radius and randomness (\irregularcircle{}{}) in axis units instead of cm and mm.

  • Can you show us an example? – Torbjørn T. Jan 16 '18 at 17:16
  • @TorbjørnT. sure, see the edit – filippo Jan 16 '18 at 17:25
  • You basically just need to do this tex.stackexchange.com/a/323489, and remove all explicit lengths (pt, mm, cm). – Torbjørn T. Jan 16 '18 at 17:35
  • @TorbjørnT. thanks, completely missed those pt lengths in the macro. But while this could fix this specific example, it doesn't answer how to get the unit length from the current plot in axis coordinates – filippo Jan 16 '18 at 18:59
  • 1
    But why do you need it? If you use \irregularcircle{2}{0.2} for example, and +(axis direction cs:{cos(\a)*\len},{sin(\a)*\len}) in the macro, that coordinate will be in axis units, so your "circle" will have a radius of 2 in the axis units. – Torbjørn T. Jan 16 '18 at 19:57

Your Answer

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

Browse other questions tagged or ask your own question.