6

When I create a figure using tikzpicture, tikz adds two 0's at the origin for a plot (one for the x-axis and another for the y-axis). I think it looks cleaner to only have one zero at the origin. I don't want to manually adjust xtick and ytick and was hoping for a smarter way to do this. Did I miss something in the PGFPlots manual?

Thanks!

  • 1
    What should happen when the two axes don't cross at the origin but 0 lies in the x-range and y-range of the data? Surely you would want one zero for each axis in such a case... – jub0bs Nov 3 '14 at 18:53
  • 1
    It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that reproduces the problem. – Peter Grill Nov 4 '14 at 1:40
4

There's no built-in code for this, but you can create a style to do it if you want. Here, I've created ignore zero which accepts a single argument: the axis for which the zero ticklabel will not be plotted:

\pgfplotsset{ignore zero/.style={%
  #1ticklabel={\ifdim\tick pt=0pt \else\pgfmathprintnumber{\tick}\fi}
}}

That just compares the current value of \tick on the specified axis to zero. If it is zero, we do nothing; if it's not zero, proceed with normal ticklabel typesetting (\pgfmathprintnumber{\tick}). Here's an example showing both possibilities (ignoring either x or y):

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\pgfplotsset{ignore zero/.style={%
  #1ticklabel={\ifdim\tick pt=0pt \else\pgfmathprintnumber{\tick}\fi}
}}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=0,ymin=0,domain=0:5,ignore zero=x]
  \addplot {x};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[xmin=0,ymin=0,domain=0:5,ignore zero=y]
  \addplot {x};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here enter image description here

  • You can further improve the apperance by modifying the labels. Say you have ignore zero=y, than you can say xtick={0,1,2,3,4,5} and xticklabels={{0~~~},1,2,3,4,5}. No really nice coding but it gets the job done. – lactea Jun 10 '16 at 8:35

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.