Mathematica takes the standard color code for atoms when plotting an *.xyz file, but I want to customize the color for the specific atoms.

I really become desperate as I did not get it so far.




It doesn't really matter which colors are used. I just need to know how to change the default ones.


3 Answers 3


One can use the (undocumented?) option ColorRules:

 ColorRules -> {"H" -> Red, "C" -> Black, "N" -> Darker@Green, "O" -> White}]

Mathematica graphics

Addendum: Other options may be found here: Options[Graphics`MoleculePlotDump`iMoleculePlot3D]. Note: The option ColorFunction seems to be unimplemented.

  • 2
    $\begingroup$ Related: 24045. $\endgroup$
    – Michael E2
    Commented Nov 12, 2014 at 17:11
  • $\begingroup$ Great find! For the particular problem I can't imagine a better answer. I still do like the generality of my approach and I hope people find value in it elsewhere. $\endgroup$
    – Mr.Wizard
    Commented Nov 13, 2014 at 9:34
  • $\begingroup$ @Mr.Wizard Thanks. I liked your answer so much, I nearly gave up after reading it. But I was curious how the bonds are determined, when bond data is missing in the xyz format. So I kept looking. $\endgroup$
    – Michael E2
    Commented Nov 13, 2014 at 12:35

Try this:

t = Import["c:\\work\\temp\\1.xyz"];

enter image description here

allColors = {};
(*get all colors from Graphics3D*)
Scan[If[MatchQ[#, RGBColor[__]], AppendTo[allColors, #]] &, t, 
allColors = DeleteDuplicates[allColors]
(*{RGBColor[0.65, 0.7, 0.7], RGBColor[0.4, 0.4, 0.4]}*)
(*replace all obtained colors with any another*)
replaceRules = MapThread[Rule, {allColors, {RGBColor[0.4, 0.4, 1], RGBColor[0.4, 1, 1]}}];
t /. replaceRules

enter image description here


This question is closely related to: How to change element color in Periodic Table? and my solution there works here as well. However thanks to this question I realized that it had a bug which I have now fixed.

An example for your application:

(* please load code from linked answer *)

  {"H" -> Blue, "O" -> Yellow, "N" -> Orange}

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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