If i import a .xyz file in Mathematica that will show me the moelcule in the default way. But i want to visualise the .xyz file in the following manner. enter image description here


2 Answers 2


Sticks and balls

file = "ben.xyz";

atm = Import[file, "VertexTypes"];
xyz = Import[file];
coord = xyz[[1, 4, 1]];
balls = Cases[xyz[[1, 4, 2]], Sphere[x_, y_] -> {x, y}];
sticks= Cases[xyz[[1, 4, 2]], Cylinder[x_, y_] -> {x, y}];
Graphics[{EdgeForm[Black], FaceForm[White],
 {Thickness[#[[2]]/1000], Line[coord[[#[[1]]]][[All, 1 ;; 2]]]} & /@ sticks,
 {Disk[coord[[#[[1]]]][[1 ;; 2]], #[[2]]]} & /@ balls,
 Text[Style[atm[[#]], 20, Bold], coord[[#, 1 ;; 2]]] & /@ Range[Length[atm]]}]

enter image description here


Here's a starting point, whose finessing/generalization I will leave to somebody else. First, import the elements and coordinates separately:

elem = Import["https://pastebin.com/raw/QqrhhvMj", {"XYZ", "VertexTypes"}];
coords = Import["https://pastebin.com/raw/QqrhhvMj", {"XYZ", "VertexCoordinates"}];

Use an undocumented internal function to get bonding information:

bonds = UndirectedEdge @@@ Graphics`MoleculePlotDump`InferBonds[elem, coords, 40, 25];

From here, you can use Graph[] for visualization:

Graph[Range[Length[elem]], bonds, 
      EdgeShapeFunction -> ({{Directive[AbsoluteThickness[4], Black], Line[#1]},
                             {Directive[AbsoluteThickness[2], White], Line[#1]}} &),
      GraphLayout -> "SpringEmbedding", 
      VertexLabels -> MapIndexed[First[#2] -> Placed[#1, Center] &, elem], 
      VertexSize -> 0.5, VertexStyle -> GrayLevel[9/10]]


Here is the corresponding diagram for Import["ExampleData/caffeine.xyz"]: caffeine


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.