# pgfplots: scatter plot with interpolation

Today I have a question about scatter plots:
Is it possible to interpolate scatter plots?
In my work I often use "intensity plots" (that's how we call them). In these plots x and y are positions, while the color of a point is determined by the intensity. This kind of plot can be created using a 3D-surface plot and setting view={0}{90}. The result can be seen in the right plot. The left plot shows the corresponding scatter plot.
Since 3D plots take a long time to create (to cite the pgfplots manual:"pgfplots’ three dimensional routines are slow"), especially for larger datasets, and since I actually don't want a 3D plot, I would like to know wheter there is a way to make the scatter plot on the left look like the 3D plot on the right.

Here is the code for my example:

\documentclass[crop,10pt]{standalone}
\usepackage[english]{babel}
\usepackage{pgfplots}
\usepgfplotslibrary{units}

\begin{document}%
\setlength{\textwidth}{246pt}%
\setlength{\linewidth}{246pt}%

\begin{tikzpicture}
\begin{axis}
coordinates {
(0,0) [0]
(0,1) [1]
(0,2) [2]
(0,3) [3]
(1,0) [4]
(1,1) [5]
(1,2) [6]
(1,3) [7]
(2,0) [8]
(2,1) [9]
(2,2) [10]
(2,3) [11]
};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[view={0}{90}]
coordinates {
(0,0,0) (1,0,4) (2,0,8)

(0,1,1) (1,1,5) (2,1,9)

(0,2,2) (1,2,6) (2,2,10)

(0,3,3) (1,3,7) (2,3,11)
};
\end{axis}
\end{tikzpicture}

\end{document}


John

-
Welcome at TeX.SX! What precisely is your goal? If you want to avoid performance problems due to 3d data, you should stick with the right plot - it can be compiled faster and results in a smaller pdf. Note that you can use the syntax of your second plot for both surface and scatter plots (using scatter src=f(x) for the scatter plot) – Christian Feuersänger Nov 9 '12 at 13:21
Sorry, I don't really understand, what you mean. The RIGHT plot is the 3D plot, viewed from above. But I fear using 1000+ data points, compilation for this will take long. That's why I wanted to do it in 2D. I want it to look like the right plot without having to use addplot3 – Janek Nov 9 '12 at 14:01
I see. But then scatter plots is the wrong approach: marks require longer than a surface plot and they result in larger pdf documents. The shader=interp approach is the most compact form and is relatively fast. The problem which causes long processing times is the number of data points (which is probably much larger for a scatter plot, by the way). There is just one way to reduce time: to generate the surface plot somehow, save it as image, and import it using \addplot graphics or \addplot3 graphics. Or to compile the image's pdf just ones and include it often (external lib) – Christian Feuersänger Nov 9 '12 at 15:34
Ok, I see. Actually, so far I've always created the images externally. The problem is, that for using \addplot graphics one always has to set the axes minima and maxima manually, which is time consuming if they are different from plot to plot. And compiling the plots just once in an extra pdf will result in different axis and tick label sizes when I change the size of the image in the final pdf. – Janek Nov 9 '12 at 15:49

1. you have some 3d visualization which requires lots of time.

2. "lots of" means 1000+ data points. This corresponds to a resolution of ~ 30x30

3. you are wondering how to improve speed; and scatter plots appeared to be a solution.

First, concerning (3.): if you need scatter plots, there is not much choice, I guess. But if you really have the choice, you should stick with surf, shader=interp. This surface plot handler can be processed efficiently by pgfplots; it is much faster than scatter plots and it results in a smaller pdf. And: if you have a relatively smooth function, it requires few data points.

Concerning the need to improve compilation times: I think there are three choices:

choice 1: the external library. Write

 \usetikzlibrary{external}
\tikzexternalize


into your preamble; then compile with pdflatex -shell-escape. This allows automatic export of individual pictures to pdf, with sophisticated logic to preserve scaling, alignment, bounding boxes, labels, etc. You can find lots of instructions in the manual or on this site.

choice 2: the standalone package can also be used. Details in the manual or on this site.

choice 3: if even the compilation of these external pdfs takes too long, you can consider reducing the sampling resolution. Perhaps this is feasible.

If the quality degenerates but you know that you surface is smooth, you could even resort to the patchplots lib of pgfplots and use some higher-order shader (patch type=bilinear or patch type=biquadratic or patch type=bicubiccombined withshader=interp). Except forpatch type=bilinear, these patches require changes to your sampling routine (i.e. the expected input changes). See alsopatch type sampling in pgfplots 1.7.

choice 4: you can resort to \addplot graphics. The \addplot graphics` switch, however, should be regarded as last hope as it involves more manual work (tuning axis limits) than desired and involves 3rd party tools (more overhead).

-