9
$\begingroup$

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.

Example:

Import["ExampleData/caffeine.xyz"]

caffeine

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

$\endgroup$
0

3 Answers 3

13
$\begingroup$

One can use the (undocumented?) option ColorRules:

Import["ExampleData/caffeine.xyz", 
 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.

$\endgroup$
3
  • 2
    $\begingroup$ Related: 24045. $\endgroup$
    – Michael E2
    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
    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
    Nov 13, 2014 at 12:35
10
$\begingroup$

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, 
  Infinity];
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

$\endgroup$
0
6
$\begingroup$

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 *)

withScheme[
  Import["ExampleData/caffeine.xyz"],
  "Atoms",
  {"H" -> Blue, "O" -> Yellow, "N" -> Orange}
]

enter image description here

$\endgroup$

Your Answer

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

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