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 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

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\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 )}; 
\end{axis}
\end{tikzpicture}
\end{document}

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

1 Answer 1

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.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    title = {Number line},
    axis y line=none,
    y=0.5cm/3,
    restrict y to domain=0:1,
    axis lines=left,
    enlarge x limits=upper,
    scatter/classes={
        o={mark=*,fill=white}
    },
    scatter,
    scatter src=explicit symbolic,
    every axis plot post/.style={mark=*,thick},
    legend style={
        draw=none,
        at={(1,1)},
        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}
\end{axis}
\end{tikzpicture}
\end{document}
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
1  
@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

 
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.