4

I am using pgfplots to plot simple functions. For example,

\begin{tikzpicture}
\begin{axis}[grid=major,xmin=0,xmax=1,ymin=0,ymax=0.5]
\addplot[color=black] coordinates {
(0,0.2)
(0.5,0.2)
(1,0)
};
\end{axis}
\end{tikzpicture}

I want to add a letter, say "x", at the point 0.5 on the horizontal axis to make some emphasis. Anyone knows how to do this?

  • Do you just want the letter "x", or do you also want to draw a vertical line or something like that? Should the letter be aligned like the tick numbers, or should it be inside the axis? – Jake Jun 22 '13 at 21:45
  • @Jake I think it would be better to put the letter inside the axis, because sometimes this emphasis might coincide with the original labels. – Richie Jun 22 '13 at 22:38
9

You can use the extra ... options:

extra x ticks={0.5},
extra x tick labels={$x$},

enter image description here

Notes:

  • As per Qrrbrbirlbel's suggestion, the extra tick labels are now aligned with the other labels.
  • As requested in the comments I have added multiple extra tick marks.

Code:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=major,xmin=0,xmax=1,ymin=0,ymax=0.5,
  extra x ticks={0.1,0.5,0.7},
  extra x tick labels={$x$,$y$,$z$},
  extra x tick style={%
     text height=\heightof{0},% Align labels at baseline
     color=red,
  },
]
\addplot[color=black] coordinates {
  (0,0.2)
  (0.5,0.2)
  (1,0)
};
\end{axis}
\end{tikzpicture}
\end{document}
| improve this answer | |
  • Can we align the x to the baselines from the other ticks? extra x tick style={text height=\heightof{0}} is one way, but not very nice. – Qrrbrbirlbel Jun 22 '13 at 22:40
  • @PeterGrill Thanks a lot. But if I want to add two or more labels, I tried that extra x ticks={0.1,0.5,0.7}, extra x tick labels={$x$,$y$,$z$} does not work. Do you know how to do this? – Richie Jun 22 '13 at 22:40
  • @Qrrbrbirlbel: Good point, but I was not able to find another way to do that. – Peter Grill Jun 22 '13 at 22:54
  • @Richie: Have updated solution to show that it can be extended to multiple labels. Perhaps you are using an older release. I am using the latest version in TeXLive2012. – Peter Grill Jun 22 '13 at 22:55
  • 1
    @LeonMeier: No, it's a new syntax. It's explained in pages 937 and 938 from pgfmanual.pdf. height("0") gives you the height of whatever is inside quotes. Similar functions are depth and width. – Ignasi Mar 10 '17 at 8:04

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.