10
$\begingroup$

I am looking for a way to add a legend showing the identity of various atoms (with different colours) to this picture. Any Clues?

Import["ExampleData/1PPT.pdb", "Rendering" -> "BallAndStick"]

enter image description here

$\endgroup$

3 Answers 3

11
$\begingroup$
bas = Import["ExampleData/1PPT.pdb", "Rendering" -> "BallAndStick", 
   ImageSize -> 500];

elements = 
  Import["ExampleData/1PPT.pdb", "ResidueAtoms"] // Flatten // Union;

legend = GraphicsColumn[{
    {Graphics[{#[[1]], Disk[{0, 0}, 1]}, ImageSize -> 10], #[[2]]} & /@
     Thread[{
       ElementData[#, "IconColor"] & /@ elements,
       ElementData[#, "StandardName"] & /@ elements}]}];

Row[{bas, legend}]

enter image description here

Alternative legend:

legend = GraphicsColumn[
   Row[{Graphics[{#[[1]], Disk[{0, 0}, 1]}, ImageSize -> 10], 
       " " <> #[[2]]}] & /@ 
    Thread[{ElementData[#, "IconColor"] & /@ elements, 
      ElementData[#, "StandardName"] & /@ elements}], 
   ImageSize -> 100];

Row[{bas, legend}]

enter image description here

$\endgroup$
1
  • $\begingroup$ +1 and thanks. Would you try to remove the distracting brackets around the legend? $\endgroup$
    – eldo
    Commented Sep 13, 2014 at 3:02
18
$\begingroup$

Bob Hanlon's answer works very well, but in some ways it is the hard way of doing things. If you have v9 or v10, then it is arguably easier to use the legend constructs within it. Similar to his answer, we get the image and element names:

img = Import["ExampleData/1PPT.pdb", "Rendering" -> "BallAndStick", ImageSize -> 500];
elements = Import["ExampleData/1PPT.pdb", "ResidueAtoms"] // Flatten // Union;

But, we deviate with the legend construction, e.g.

legend = SwatchLegend[
  ElementData[#, "IconColor"] & /@ elements, 
  ElementData[#, "StandardName"] & /@ elements, 
  LegendMarkers -> Graphics@Disk[]
]

and to display the legend, we use

Legended[img, legend]

enter image description here

At this point, there is not much in the way of savings, but it simplifies a number of things, such as repositioning the legend using Placed, or adding additional legends. For instance, what if the pdb file also displayed hydrogen bonds, in addition to covalent bonds, then it is straightforward to add a second legend:

legendRow = SwatchLegend[
  ElementData[#, "IconColor"] & /@ elements, 
  ElementData[#, "StandardName"] & /@ elements, 
  LegendMarkers -> Graphics@Disk[],
  LegendLayout -> "Row"
];
legendBonds = LineLegend[
  {Gray, Directive[Red, Dashed]}, 
  {"Covalent", "Hydrogen"}
];

Legended[img, {Placed[legendRow, Top], legendBonds}]

enter image description here

$\endgroup$
6
$\begingroup$

To show the colors associated with atoms, use the Automatic setting for the PlotLegends option:

MoleculePlot3D[
    Molecule @ "1-(amino-dimethyl-silyl)-2,3,4,5,6-pentafluoro-benzene", 
    PlotLegends -> Automatic
]

enter image description here

The example in the OP does not work yet, but the PDB importer will be updated in a future release.

$\endgroup$

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.