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.

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 have a data file with many entries, order of 10000. I want to plot them all with lines, and a small subset of them with points and error bars (can not plot error bars on 10000 samples). Plotting each N is easy done using each nth point=N, but I am using a semilogarithmic axis, so I want to plot each 10^N (even better 2^N).

Right now I preprocess the data file and generate another one with the interesting samples subset, is there a way to make pgfplots do that?


This is a minimal working example:




    \addplot+ table[col sep=comma, x=x, y=y, each nth point=100]


File aaa.txt is something like this:


I generated the coordinates with Ruby's script 1.step(1024) { |i| puts "#{i},#{i},#{rand}" }. With using nth point=100 it plots only at 100, 200, 300... I tried replacing it with the x filter suggested but it keeps plotting the whole data file (it does not say on command line that it is skipping points because a filter, as it does for using nth point).

share|improve this question
up vote 5 down vote accepted

Replacing each nth point=N with the following should work :

x filter/.code={\pgfmathparse{divide(#1,equal(#1,floor(#1))}}

Some explanation : x filter allows, as the name says, coordinate filtering. The code is provided the coordinate as #1 (or its log in case the axis is logarithmic, which is the case here), and performs some computation on it. If the result is a number, it is used as the coordinate, if not, it is discarded.

Here we want to discard the cases where log(x) isn't an integer, so we test equality between log(x) and Ceil(log(x)). If they aren't equal, the code performs a division by zero, which results in inf and is discarded. If they are equal, on the other hand, dividing log(x) by 1 gives back log(x).

Be sure to add log basis x=2 in your semilogxaxis environment options if you want to filter powers of 2.

Now that's for the theory. However, if you try the above code with your example, it won't work, because pgfmath computes the logarithm in base 2 wit poor roundings, e.g. log2(8)=2.99997. The following code does work. Please keep in mind that it still relies on approximate calculations, and as such, that it may fail for bigger values.




  \begin{semilogxaxis}[unbounded coords=discard,
    log basis x=2,
    x filter/.code={\pgfmathparse{divide(#1,(equal(2^(#1),2^(round(#1)))))}}
  \addplot+ table[col sep=comma,x=x, y=y]

share|improve this answer
I realise that depending on how your data is arranged, the code might require some heavy tweaking. Please provide a MWE as well as a sample of your data (as an edit to your question) if you need a more specific answer. – T. Verron Nov 9 '12 at 21:46
Sorry for the late answer, I had to fix more important things and I left this behind for few days. I tried your suggestion, read the doc about x filter, but I could not get it work, it plots always all the points. I added a MWE in my question. – Claudio Nov 19 '12 at 14:02
@Claudio : Editted with a working answer. – T. Verron Nov 19 '12 at 16:01

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.