3

Say my plot shows geometric objects in a metric x,y plane, so one axis unit equals 1m. Drawing in metric units is easy using axis cs for the coordinates like in this example:

\begin{tikzpicture}
        \begin{axis}[
            xmin=0,
            xmax=20,
            ymin=0,
            ymax=20,
            axis lines=center,
            axis equal
            ]
            \draw[line width=10mm] (axis cs: 10,0) -- (axis cs: 10,10);
        \end{axis}
    \end{tikzpicture}

Is there a way to define the width of the line in axis coordinate units also (maybe with veclen or so)? \draw[line width=10mm] seems to define the width in the document target unit, \draw[line width=10] in some kind of pixel unit.

  • what do you mean by an axis coordinate unit? could you please clarify? Cant you use 10pt instead? – Raaja Mar 19 at 14:29
  • @Raaja As described in the question, my axis unit represents meters (one step along an axis equals 1 meter). Now I want my line to have a width of 10 meters, or 10 axis units respectively. 10pt yields the same result as 10. – F1iX Mar 19 at 14:44
4

Sure. (In sufficiently recent versions of pgfplots axis cs: is no longer needed.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
        \begin{axis}[
            xmin=0,
            xmax=20,
            ymin=0,
            ymax=20,
            axis lines=center,
            axis equal
            ]
            \draw let \p1=($(axis cs: 1,0)-(axis cs: 0,0)$) in 
            % \x1 is now one axis x unit
            [line width=2*\x1] (axis cs: 10,0) -- (axis cs: 10,10);
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

  • Thanks, this works (in my example line width=10*\x1 produces the desired line with line width 10 meters)! – F1iX Mar 19 at 14:50

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.