TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to plot a series of sets of intervals (and points) in the real line. Intervals can be closed, left/right-open or open. (Open/closed ends are marked with empty/filled circles) and isolated points are just dots. Groups of intervals may have a common style and a common legend. Below you can see a mock up.

enter image description here

I can get the basics with the following code


\begin{axis}[title = {Number line}, axis y line=none, y=0.5cm/3, restrict y to domain=0:20, axis lines=left]
\addplot+[only marks] coordinates  {( 3.14, 0 )( 10, 0 )}; 
\addplot+[only marks] coordinates  {( 20, 0 ) ( 21, 0 )}; 

but it is difficult to go further, because I couldn't find a way to change the style of the point (e.g. from filled to empty circles) within a coordinates and/or break a single coordinate set to plot two disjoint intervals and points with the same style (like the blue set in the picture).

And alternative that I found is to use tikz draws inside the axis environment

\draw[green, -|] (axis cs:5,0)--(axis cs:6,0); % something like "(-]" or "*-o" (if they worked) may be better

but the points are ignored to set the axis boundaries (which I need to be automatic). Also I need to use pgfplots to keep consistency with other (normal) plots (in terms of the style of the point and lines and the axis).

share|improve this question
up vote 12 down vote accepted

If you use a table instead of coordinates, an empty line tells pgfplots not to connect the points. To change the marker style, you can use the scatter/classes option. Note that this will create one legend entry per scatter class, so in this case, you should only define one.


    title = {Number line},
    axis y line=none,
    restrict y to domain=0:1,
    axis lines=left,
    enlarge x limits=upper,
    scatter src=explicit symbolic,
    every axis plot post/.style={mark=*,thick},
    legend style={
        anchor=south east
    legend image post style={mark=none}
\addplot table [y expr=0,meta index=1, header=false] {
3.14 c
10 o

13 c
15 c

17 c
};\addlegendentry{Set 1}
\addplot table [y expr=0,meta index=1, header=false] {
20 c
21 c
};\addlegendentry{Set 2}
share|improve this answer
Many thanks. Note for others: the example code needs at least pgfplots 1.4.1 to work, as it needs to accept inline tables. – alfC Feb 19 '12 at 8:36
Also, legend entries don't work as expected because each break inside table is assigned to a different legend entry and I don't know how to set a forget plot to parts of the table. – alfC Feb 19 '12 at 9:00
@alfC: It's not because of the breaks, it's because of the scatter classes. I've edited my answer. – Jake Feb 19 '12 at 11:03

Your Answer


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.