10

I'm plotting something using matplotlib and I'm using the matplotlib.cm.rainbow colormap. I need to match the same colors in my LaTeX document, but I'm having a difficult time making the colors match between what I get on my plot and what appears in my LaTeX document.

What do I have to do to get matplotlib and LaTeX to use the same colors? Even coming close would be a benefit.

The code I'm using to plot in matplotlib is:

import matplotlib.pyplot as pyplot

Fig, ax = pyplot.subplots()
indices = range(8)

length = len(indices)
print("\ncolors: ")
for i in range(length-1):
    left = indices[i]
    right = indices[i+1]

    c = pyplot.cm.rainbow(i/length)
    print("i={}, color={}".format(i,c))
    ax.fill_between((left,right), 0,1,
                    facecolor=c, edgecolor=c, alpha=0.25)

# Do the last one
i +=1
left = indices[i]
right = 8
c = pyplot.cm.rainbow(1.0)
print("i={}, color={}".format(i,c))
ax.fill_between((left,right), 0,1,
                facecolor=c, edgecolor=c, alpha=0.25)

The output from this is:

colors: 
i=0, color=(0.5, 0.0, 1.0, 1.0)
i=1, color=(0.24901960784313726, 0.38410574917192586, 0.98063477046897773, 1.0)
i=2, color=(0.0019607843137254832, 0.70928130760585339, 0.92328910610548931, 1.0)
i=3, color=(0.25294117647058822, 0.92563765978155632, 0.83018403081555059, 1.0)
i=4, color=(0.50392156862745097, 0.99998102734872685, 0.70492554690614728, 1.0)
i=5, color=(0.75490196078431371, 0.92090551794495368, 0.55236497296050591, 1.0)
i=6, color=(1.0, 0.70054303759329106, 0.37841105004231035, 1.0)
i=7, color=(1.0, 1.2246467991473532e-16, 6.123233995736766e-17, 1.0)

matplotlib plot output

I use the output to define colors in LaTeX using:

\definecolor{colorA}{cmyk}{0.5    , 0.0    , 1.0     , 0.0}
\definecolor{colorB}{cmyk}{0.2490 , 0.3841 , 0.9806  , 0.0}
\definecolor{colorC}{cmyk}{0.0019 , 0.7092 , 0.9232  , 0.0}
\definecolor{colorD}{cmyk}{0.2529 , 0.9256 , 0.8301  , 0.0}
\definecolor{colorE}{cmyk}{0.5039 , 0.9999 , 0.7049  , 0.0}
\definecolor{colorF}{cmyk}{0.7549 , 0.9209 , 0.5523  , 0.0}
\definecolor{colorG}{cmyk}{1.0    , 0.7005 , 0.37841 , 0.0}
\definecolor{colorH}{cmyk}{1.0    , 1.2246 , 6.12323 , 0.0}

Finally I use the colors in a table (with the colortbl package) using the command

\rowcolor{colorA!25} 1 & ... 

However, the colors in the figure don't match the colors in the table and it is aggravating me why it doesn't work. Instead of the purple, blue, aqua, ... (I know these are not technical color names) that is seen in the figure, the colors I'm getting are green, orange, orange red, red, gray red, violet, blue,... The colors are not even close.

migrated from stackoverflow.com Sep 19 '13 at 5:57

This question came from our site for professional and enthusiast programmers.

  • good luck;) I think this is a LaTeX issue, I had great trouble matching colors to pdf/eps files. You might have luck on latex stack exchange as well. – tacaswell Sep 18 '13 at 1:25
6

Your problem is simply that pyplot.cm.rainbow does not return CMYK values, but RGB + alpha. An excerpt from the docstring:

Returns

Tuple of RGBA values if X is scalar, othewise an array of RGBA values with a shape of X.shape + (4, ).

Hence, by using

\definecolor{colorname}{rgb}{a,b,c}

where a,b,c are the first three values from rainbow, the colours match. (For colorH note that the g and b values are (very close to) 0, not 1.22 and 6.12, but that was perhaps just a copy-paste error.)

\documentclass{article}
\usepackage{graphicx}
\usepackage[table]{xcolor}
\definecolor{colorA}{rgb}{0.5    , 0.0    , 1.0     }
\definecolor{colorB}{rgb}{0.2490 , 0.3841 , 0.9806  }
\definecolor{colorC}{rgb}{0.0019 , 0.7092 , 0.9232  }
\definecolor{colorD}{rgb}{0.2529 , 0.9256 , 0.8301  }
\definecolor{colorE}{rgb}{0.5039 , 0.9999 , 0.7049  }
\definecolor{colorF}{rgb}{0.7549 , 0.9209 , 0.5523  }
\definecolor{colorG}{rgb}{1.0    , 0.7005 , 0.37841 }
\definecolor{colorH}{rgb}{1.0    , 0.0    , 0.0     }
\begin{document}
\includegraphics{colors}

\begin{tabular}{*{8}{c}}
\cellcolor{colorA!25}\phantom{\rule{1cm}{4cm}} &
\cellcolor{colorB!25}\phantom{\rule{1cm}{4cm}} &
\cellcolor{colorC!25}\phantom{\rule{1cm}{4cm}} &
\cellcolor{colorD!25}\phantom{\rule{1cm}{4cm}} &
\cellcolor{colorE!25}\phantom{\rule{1cm}{4cm}} &
\cellcolor{colorF!25}\phantom{\rule{1cm}{4cm}} &
\cellcolor{colorG!25}\phantom{\rule{1cm}{4cm}} &
\cellcolor{colorH!25}\phantom{\rule{1cm}{4cm}} 
\end{tabular}
\end{document}

enter image description here

  • 2
    This is the kind of answer making this forum so valuable: down to the very detail of the technic. – Keks Dose Sep 19 '13 at 14:42

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.