Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to change the default axes arrowheads size of pgfplots when using the option axis lines=center. Following this answer, I tried this code:

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows, decorations.markings}

\begin{document}
\begin{tikzpicture}
\tikzstyle{myaxis} = [decoration={markings, mark=at position 1 with {\arrow[ultra thick]{latex'}}},
                      postaction={decorate}, shorten >=0.4, line width=0.02pt];
\begin{axis}[axis lines=center, axis line style={myaxis}]
\end{axis}
\end{tikzpicture}
\end{document}

but it doesn't work as I would expect, since the axes lines aren't drawn at all.

Could you help me? Thanks,

Luigi

share|improve this question

2 Answers 2

up vote 10 down vote accepted

Customising axis lines is a bit unintuitive: For one, the decoration to be used in the postaction needs to be specified within the postaction. And applying a postaction to the axes when using axis lines=left (instead of axis lines=middle, for example) requires the use of the every path/.style (similar to what was done in How to specify a name path for the axis in PGFplots). To avoid an infinite recursion in this case, you need to clear the postaction once it's been executed for the first time. This can be done using the nomorepostaction key described in Applying a postaction to every path in TikZ.

Here's your axis generated using

\begin{axis}[
    axis lines=middle,
    axis line style={my axis}
]
\addplot coordinates {(-0.1,-0.2) (1.2,1.2)};
\end{axis}

And here's the complete code:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows, decorations.markings}

\begin{document}
\begin{tikzpicture}

\makeatletter
\tikzset{
    nomorepostaction/.code=\makeatletter\let\tikz@postactions\pgfutil@empty, % From http://tex.stackexchange.com/questions/3184/applying-a-postaction-to-every-path-in-tikz/5354#5354
    my axis/.style={
        postaction={
            decoration={
                markings,
                mark=at position 1 with {
                    \arrow[ultra thick]{latex}
                }
            },
            decorate,
            nomorepostaction
        },
        thin,
        -, % switch off other arrow tips
        every path/.append style=my axis % this is necessary so it works both with "axis lines=left" and "axis lines=middle"
    }
}
\makeatother


\begin{axis}[
    axis lines=middle,
    axis line style={my axis}
]
\addplot coordinates {(-0.1,-0.2) (1.2,1.2)};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
Thank you for you answer. It was what I asked for till I learnt to edit the arrowhead style by myself. –  Luigi May 16 '12 at 10:25
    
@Liverpool: Pleasure. Editing the arrowhead style is certainly an option, but I'm not sure it's a better approach per se. With the decoration, you're a lot more flexible: you can change the arrow type without having to declare a new arrowhead, you could use a different colour for the tip, and you can set the size just as well by using line width=<size>. –  Jake May 16 '12 at 10:29
    
you're right, but if you need an open arrowhead, it could be difficult to estimate how much to shorten the line such that it doesn't cut the tip. It all started by this question on the Italian users' TeX group (guitex.org/home/it/forum/5-tex-e-latex/…) –  Luigi May 16 '12 at 13:00

I tried this solution and I think it's better for me. I edited the definition of the latex' arrowhead so that you can specify directly the tip length or use the default value (8pt). So the arrowhead size is now no more dependent on the line width. Obviously you can use the same solution in plain TikZ.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfarrowsdeclare{mylatex'}{mylatex'}
{
  \newdimen\len
  \len=\pgfgetarrowoptions{mylatex'}
  \pgfarrowsleftextend{-0.4\len}
  \pgfarrowsrightextend{0.6\len}
}
{
  \newdimen\len
  \len=\pgfgetarrowoptions{mylatex'}
  \pgfpathmoveto{\pgfqpoint{0.6\len}{0\len}}
  \pgfpathcurveto
  {\pgfqpoint{0.35\len}{0.05\len}}
  {\pgfqpoint{-0.1\len}{0.15\len}}
  {\pgfqpoint{-0.4\len}{0.375\len}}
  \pgfpathcurveto
  {\pgfqpoint{-0.15\len}{0.1\len}}
  {\pgfqpoint{-0.15\len}{-0.1\len}}
  {\pgfqpoint{-0.4\len}{-0.375\len}}
  \pgfpathcurveto
  {\pgfqpoint{-0.1\len}{-0.15\len}}
  {\pgfqpoint{0.35\len}{-0.05\len}}
  {\pgfqpoint{0.6\len}{0\len}}
  \pgfusepathqfill
}
\pgfsetarrowoptions{mylatex'}{8pt}
\pgfkeys{/tiplen/.default=8pt, /tiplen/.code={\pgfsetarrowoptions{mylatex'}{#1}}}

\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=left, axis line style={-mylatex'}]
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[axis lines=middle, axis line style={-mylatex', /tiplen=4pt}]
\addplot [only marks] coordinates {(0,0) (0.5,0.5)};
\draw [-mylatex', /tiplen=1cm] (axis cs: 0.1,0.1) -- (axis cs: 0.4,0.4);
\end{tikzpicture}
\end{document}

share|improve this answer
    
I'm adding a comment just to highlight that more arrows are now available here. –  Luigi Nov 3 '12 at 12:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.