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

EDIT

This is a minimal working example:

\documentclass[10pt,a4paper]{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
  \begin{semilogxaxis}
    \addplot+ table[col sep=comma, x=x, y=y, each nth point=100]
        {aaa.txt};
  \end{semilogxaxis}
\end{tikzpicture}

\end{document}

File aaa.txt is something like this:

x,y,var
1,1,0.7257899338880273
2,2,0.5103550634511824
3,3,0.8469400089552953
4,4,0.8351651602434369
5,5,0.07359721861492374
6,6,0.3107395762944515
7,7,0.9151522020717112
8,8,0.4967609141914707
9,9,0.2218284814240239
10,10,0.7166461578126254
...

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

1 Answer 1

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.

\documentclass[10pt,a4paper]{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
  \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]
    {aaa.txt};
  \end{semilogxaxis}
\end{tikzpicture}

\end{document}
share|improve this answer
1  
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

 
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.