3

I have a small problem with TikZ I can't figure out. I have some TikZ code generated in MATLAB (using matlab2tikz) showing an image with a user-defined colormap and the correspondent colorbar. The image is exported as .png but given that the colorbar is created in TikZ I need to color it accordingly (defining the new colormap). I simplified the code to get a MWE and it reads as follows (I replaced the user-defined colormap with a simple one):

\documentclass[11pt,twoside,a4paper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\usepackage{tikz}                       %inline graphics
\usetikzlibrary{plotmarks,external}   
\begin{document}

\begin{tikzpicture}

\begin{axis}[width=\textwidth,height=4cm,axis on top,scale only axis,
xmin=0,xmax=5,xlabel={myxlabel},ymin=0,ymax=4000,
colorbar,
colormap ={mymap}{rgb(0pt)=(0,1,1); rgb(63pt)=(1,0,1)},
ylabel={myylabel}
]
\end{axis}
\end{tikzpicture}%
\end{document}

This works fine, but given that my colormap has more than two colors definitions, I thought that saving it in a separate .tex file, say mycmap.tex, was a nice idea

colormap ={mymap}{\input{mycmap}},

However, if I do this it doesn't work... I tried to save it as a .txt file instead of .tex, but still no luck. Any idea on how to fix it?

Thanks in advance! ;)

1

I don't know why, but choosing colormaps with less than 64 colors worked for me.

1

You may define a macro in your external file as in the below code (filecontents package and environment used to create the external file):

\documentclass[11pt,twoside,a4paper]{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\usepackage{tikz}                       %inline graphics
\usetikzlibrary{plotmarks,external}

%% creation of external colormap
\usepackage{filecontents}
\begin{filecontents*}{\jobname-external-colormap}
\def\mycolormap{rgb(0pt)=(0,1,1); rgb(63pt)=(1,0,1)}
\end{filecontents*}
%%

\input{\jobname-external-colormap}

\begin{document}
\begin{tikzpicture}

\begin{axis}[width=\textwidth,height=4cm,axis on top,scale only axis,
xmin=0,xmax=5,xlabel={myxlabel},ymin=0,ymax=4000,
colorbar,
colormap ={mymap}{\mycolormap},
ylabel={myylabel}
]
\end{axis}
\end{tikzpicture}
\end{document}
  • Thanks for the reply Paul. I'm not really sure your solution solves my problem (which I had essentially forgotten :D), because my idea was including the colormap data from an external file. However, I've also discovered why I was getting problem when using more than 2 color points; the syntax I was using: colormap={mymap}{rgb(0pt)=(0,1,1);rgb(30pt)=(1,1,1); rgb(63pt)=(1,0,1)} doesn't work because I don't indicate the parameter [1pt] at the beginning of the color declaration. – gbernardi Jan 8 '15 at 12:18
  • It should instead be: colormap={mymap}{[1pt] rgb(0pt)=(0,1,1);rgb(30pt)=(1,1,1); rgb(63pt)=(1,0,1)} and like this it works even with more colours. – gbernardi Jan 8 '15 at 12:21
  • @gbernardi My example uses an external file (here, this external file is created by filecontents* environment to keep my code auto-sufficient). – Paul Gaborit Jan 8 '15 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.