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 want to plot some data using pgfplot. Here is a code I have written:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  legend style={legend pos = north west, font=\footnotesize, draw=none},
  legend cell align=left,
  xtick=data,
  ytick={0,5,10,...,45},
  xticklabels={0,2,4,...,16,18},
  every axis label={font=\footnotesize},
  tick label style={font=\footnotesize},
  label style={font=\footnotesize},
  enlarge x limits=false,
  enlarge y limits=false]
  \addplot[lightgray, mark=*] table [x expr = \lineno, y = Time] {Data.dat};
  \addlegendentry{x}
  \end{axis}
\end{tikzpicture}
\end{document}

The content of Data.dat file is as follows:

 Points Time
   0        0
   2000     1.11
   4000     3.54
   6000     7.35
   8000     12.43
   10000    18.15
   12000    24.51
   14000    31.52
   16000    38.59
   17791    45.35

As I'm new to this site, I'm not able to post the output graph. In the output graph, the last point (17791, 45.35) is wrongly plotted at x=18 and y=45.35.
So my question is: How do I make the last point plotted correctly between points x=16 and x=18?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

The point is plotted precisely at x=17.791, y=45.35; the problem is that the label used for the last tick is wrong (you manually declared it to be 18 and this overrides the default value of 17.8); the problem is not the plotting: the problem is the way in which you place the x labels (specifically the last one).

In the following example I show one possible way to correct the situation: I let xtick=data (so now the ticks and their labels are automatically picked from the table) and used extra x ticks and extra xtick labels with an auxiliary blue grid just to show where the point corresponding to 18 really is; I also changed the mark used (the mark you used is too big, in my opinion, and could produce the impression of being located at x=18) and also used a different color just for visualization purposes. As you can see, the x-coordinate for the last point is just where it should be (a little to the left of the red vertical line drawn for x=18):

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.3}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[
  xmax=18500,
  legend style={legend pos = north west, font=\footnotesize, draw=none},
  legend cell align=left,
  xtick=data,
   ytick={0,5,10,...,45},
   extra x ticks={18000},
   extra x tick labels={},
   extra x tick style={grid=major,tick label style={rotate=90,anchor=east}},
   major grid style={color=red},
   every axis label={font=\footnotesize},
   tick label style={font=\footnotesize},
  label style={font=\footnotesize},
  enlarge x limits=false,
  enlarge y limits=false,
  scaled x ticks=base 10:-3
]
  \addplot[blue, mark=x] table  {Data.dat};
  \addlegendentry{x}
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You can use an explicit list for xmark:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.3}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[
  xmax=18500,
  ymax=46,
  legend style={legend pos = north west, font=\footnotesize, draw=none},
  legend cell align=left,
   xtick={0,2000,4000,6000,8000,...,14000,16000,18000},
   ytick={0,5,10,...,45},
  major grid style={color=red},
  every axis label={font=\footnotesize},
  tick label style={font=\footnotesize},
  label style={font=\footnotesize},
  enlarge x limits=false,
  enlarge y limits=false,
  scaled x ticks=base 10:-3
]
  \addplot[blue, mark=x] table  {Data.dat};
  \addlegendentry{x}
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
Thanks for your instant reply. What shall I do if I would like to display last xticklabel as 18 and still plot the last point a little left to x=18? –  user1444462 Aug 18 '12 at 7:53
    
@user1444462 You're welcome! I updated my answer; if you consider that my answer solved your problem, please consider accepting it (by clicking the green checkmark below the vote count). –  Gonzalo Medina Aug 18 '12 at 14:12
    
Thanks a lot!!! That's precisely how I wanted to plot. –  user1444462 Aug 18 '12 at 17:55
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.