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.

enter image description here

The picture produced by code blow, there are two problems I have.

(1) the number above point overlapped

(2) the precision of number is not right, eg. I want 0.913, not 0.91

For the first problem I tried the 'nodes near coords align', but no good result. Is there a way to solve this?

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}
\pagestyle{empty}

\begin{filecontents}{x.dat}
1,0.304313797
2,0.558012673
3,0.741302281
4,0.851281192
5,0.912940189
6,0.945961019
7,0.964838045
8,0.975736752
9,0.982694892
\end{filecontents}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            axis x line=bottom,
            axis y line=left,
            xlabel={Query Length},
            ylabel={Percentage},
            xmin=0,
            ymin=0,
            xmax=10,
            ymax=1,
            xtick={0,1,2,3,4,5,6,7,8,9},
            ytick={0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9},
            tick align=inside,
            nodes near coords,
            %nodes near coords align=horizontal,
            ]
            \addplot table[x index=0, y index=1, col sep=comma,mark=none]
            {x.dat};
        \end{axis}
    \end{tikzpicture}
\end{document}
share|improve this question
add comment

1 Answer

up vote 8 down vote accepted

You can set the precision using /pgf/number format=3. To make the numbers fit, you can choose a smaller font and make the plot a bit wider. For nicer alignment, you can use anchor=-\coordindex*10, which will make the first node align with the 0 anchor (i.e. east), the second one with the -10 anchor (slightly below east), and so on.

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}
\pagestyle{empty}

\begin{filecontents}{x.dat}
1,0.304313797
2,0.558012673
3,0.741302281
4,0.851281192
5,0.912940189
6,0.945961019
7,0.964838045
8,0.975736752
9,0.982694892
\end{filecontents}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            axis x line*=bottom,
            axis y line*=left,
            xlabel={Query Length},
            ylabel={Percentage},
            xmin=0,
            ymin=0,
            xmax=10,
            ymax=1,
            width=10cm, height=6cm,
            xtick={0,1,2,3,4,5,6,7,8,9},
            ytick={0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9},
            tick align=inside,
            nodes near coords,
            every node near coord/.append style={
                font=\scriptsize,
                /pgf/number format/precision=3,
                anchor=-\coordindex*10}
            ]
            \addplot table[x index=0, y index=1, col sep=comma,mark=none]
            {x.dat};
        \end{axis}
    \end{tikzpicture}
\end{document}
share|improve this answer
    
thanks much for your help –  icycandy Jun 7 '12 at 7:10
1  
@icycandy: Pleasure! By the way, you can write xtick={0,...,9} and ytick={0.1,0.2,...,0.9} instead of providing the full lists. –  Jake Jun 7 '12 at 7:56
add comment

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.