11

I would like to color a surf using arbitrary RGB colors (unlike this question, which just colors the surf using the colormap).

In this minimal example (which plots the x+y function), I would like the color of each patch to be red=x, green=y, blue=x*y for example.

Bonus points if I can also set the opacity :) .

I think this can be done using point meta=explicit symbolic or point meta=Tex code symbolic, but I don't know how to use the metadata afterwards.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepgfplotslibrary{patchplots}
\begin{document}

\begin{tikzpicture}
  \begin{axis}
    \addplot3[surf] { x + y };
  \end{axis}
\end{tikzpicture}

\end{document}
Suzanne Soy
  • 3,043

1 Answers1

10

Pgfplots up to and including version 1.7 only supports colors by means of a colormap.

EDIT this restriction applies to mesh/surface plots, special scatter plots might work.

You are the second user requesting this feature. I accept that as a feature request.

It is good to know that you would like to express RGB components in dependence of the parameters x and y. I suppose one would also like to provide colors using the syntax of xcolor, so the color format should probably be flexible enough to support both.

Edit by Georges Dupéron

For those impatient to try, you can check out the (probably bleeding edge) version of Christian's pgfplots :

# Create a temporary working directory.
workdir="/tmp/$(date +%s)"
mkdir "$workdir"
# Download the latest (as of 2013-02-13) unstable version of PGF
mkdir "$workdir/pgf"
cd "$workdir/pgf";
wget http://www.texample.net/media/pgf/builds/pgfCVS2012-11-04_TDS.tgz -O- | tar zxvf -
# Download the latest version of pgfplots
cd "$workdir"
git clone git://pgfplots.git.sourceforge.net/gitroot/pgfplots/pgfplots
# Tell LaTeX to use these versions
export TEXINPUTS="$workdir/pgf/tex//:$workdir/pgfplots//:"
cd "$workdir/pgfplots"
# Add a dummy tag so we can run pgfplotsrevisionfile.sh, which is required to use pgfplots
git tag 1.7.42
./scripts/pgfplots/pgfplotsrevisionfile.sh
# Compile a small example that plots x*y with red=x, green=y and blue=0
pdflatex source/latex/pgfplots/pgfplotstest/unittests/unittest_shader_interp_explicitcolor_math.tex
# View the resulting PDF
evince unittest_shader_interp_explicitcolor_math.pdf

The result (plot x*y with mesh/color input=explicit mathparse, point meta/symbolic={x,y,0}):

plot x*y with red=x, green=y and blue=0

\documentclass[a4paper]{article}

\usepackage{pgfplots}

\usepgfplotslibrary{patchplots}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}
%\tracingmacros=2 \tracingcommands=2
\begin{axis}
    \addplot3[
        patch,
        patch type=bilinear,
        shader=interp,
        mesh/color input=explicit mathparse,
        domain=0:1,
        samples=5,
        point meta/symbolic={x,y,0}
        ]
    {x*y};
\end{axis}
\end{tikzpicture}

\end{document}

Another example (plot sin(deg(x*pi*2))+sin(deg(y*pi*2)) with mesh/color input=explicit mathparse, point meta/symbolic={(sin(deg(x*pi*2))+1)/2,(sin(deg(y*pi*2))+1)/2,0}, after having plot -3 with the same colors):

plot sin(x)+sin(y) with red=sin(x), green=sin(y) and blue=0, and -3 with the same colors

\documentclass[a4paper]{article}

\usepackage{pgfplots}

\usepgfplotslibrary{patchplots}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}
%\tracingmacros=2 \tracingcommands=2
\begin{axis}
    \addplot3[
        patch,
        patch type=bilinear,
        shader=faceted interp,
        mesh/color input=explicit mathparse,
        domain=0:1,
        samples=30,
        point meta/symbolic={(sin(deg(x*pi*2))+1)/2,(sin(deg(y*pi*2))+1)/2,0}
        ]
    {-3};
    \addplot3[
        patch,
        patch type=bilinear,
        shader=faceted interp,
        mesh/color input=explicit mathparse,
        domain=0:1,
        samples=30,
        point meta/symbolic={(sin(deg(x*pi*2))+1)/2,(sin(deg(y*pi*2))+1)/2,0}
        ]
    {sin(deg(x*pi*2))+sin(deg(y*pi*2))};
\end{axis}
\end{tikzpicture}

\end{document}
Suzanne Soy
  • 3,043
  • Section 4.7 of the documentation says "However, point meta can be anything. [...] It could also contain a tuple like RGB color information (which is not commonly used, however).", so I thought it was possible. It must be the first time I see future features in a documentation, usually documentations are outdated :-p . Thank you for the information. I'll wait a little before I accept this answer, in case someone posts a patch/workaround. – Suzanne Soy Feb 10 '13 at 16:53
  • 1
    Since I enjoy that shading stuff, I started to work on a prototype for explicit colors and I got the first positive results (with shader=flat corner and shader=interp). – Christian Feuersänger Feb 10 '13 at 21:09
  • Note that the remark with "RGB color information" might actually make sense for special scatter plots. Surface plots are somewhat special since they really need to interpolate between "adjacent" colors. – Christian Feuersänger Feb 10 '13 at 21:10
  • My use case for that is to plot functions from complex to complex, with z=real(f(x,y))+imag(f(x,y)) (or magnitude, or whatever transformation from complex to real), and red=real(f(x,y)) with green=imag(f(x,y)) to get the "exact" real/imaginary information in the color. – Suzanne Soy Feb 10 '13 at 21:17
  • @GeorgesDupéron I see. I suppose that the result of your color components mappings for red and green are in the unit interval, right? – Christian Feuersänger Feb 10 '13 at 21:20
  • Yes, most of the time (and otherwise I can scale the values "by hand"). – Suzanne Soy Feb 10 '13 at 22:04
  • I managed use your recent changes to pgfplot's git repository, and the results are great, so I took the the freedom to add the steps to download & use the latest version (commit git checkout 79bd3f979d0f117408172f0fc17cba5b19a5fd0b, forgot to include that in my edit) to your post, I hope you don't mind. The edit hasn't been accepted yet (I haven't got enough rep, so it must be peer reviewed first). – Suzanne Soy Feb 13 '13 at 22:30
  • @GeorgesDupéron that looks quite cool; actually better than I envisioned. Could you please include the source code for the final examples such that others can reproduce them? The syntax of your revision is final; I do not intent to change it before the release. – Christian Feuersänger Feb 15 '13 at 21:01
  • There is a bug/conflict in the current commit (f84332ffd9b5096ee892af3ea9818f0eaee74a4f) : Adding a \node[fill opacity=0.75]{a}; at the end of the tikzpicture of the first example will cause the colors to "burn" (dark colors become much darker) on adobe acrobat reader. I think it is a colorspace issue. – Suzanne Soy Feb 15 '13 at 22:00
  • @GeorgesDupéron ok, good to know. I suggest we continue this conversation off-topic. Please send me an email for any issues you encounter while working with this prototype (address can be found in the pgfplots manual). – Christian Feuersänger Feb 16 '13 at 10:48
  • 2
    @GeorgesDupéron I finished the documentation for that feature (currently available as unstable on http://pgfplots.sourceforge.net/). I found (only) a work-around for the color burning: to switch the color interpolation to CMYK; see also the manual (I took that example to document the issue). – Christian Feuersänger Mar 10 '13 at 15:00