Attaching a legend to BallAndStick picture
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]
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}]
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}]
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}]
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
]
The example in the OP does not work yet, but the PDB importer will be updated in a future release.