@@ -337,19 +337,17 @@ See {ref}`sec_tskit_viz_dynamic_effects` if you want to dynamically hide and sho
337337labels.
338338
339339``` {code-cell} ipython3
340- nd_labels = {} # An array of labels for the nodes
341- for n in ts_mutated.nodes():
340+ nd_labels = { # An array of labels for the nodes
342341 # Set sample node labels from metadata. Here we use the population name, but you might want
343342 # to use the *individual* name instead, if the individuals in your tree sequence have names
344- if n.is_sample():
345- nd_labels[n.id] = ts_mutated.population(n.population).metadata["name"]
343+ n.id: ts_mutated.population(n.population).metadata["name"]
344+ for n in ts_mutated.nodes()
345+ if n.is_sample()
346+ }
346347
347- mut_labels = {} # An array of labels for the mutations
348- for mut in ts_mutated.mutations(): # Make pretty labels showing the change in state
349- site = ts_mutated.site(mut.site)
350- older_mut = mut.parent >= 0 # is there an older mutation at the same position?
351- prev = ts_mutated.mutation(mut.parent).derived_state if older_mut else site.ancestral_state
352- mut_labels[mut.id] = f"{prev}→{mut.derived_state}"
348+ mut_labels = { # An array of labels for the mutations
349+ mut.id: f"{mut.inherited_state}→{mut.derived_state}" for mut in ts_mutated.mutations()
350+ }
353351
354352ts_mutated.draw_svg(
355353 size=(1000, 300),
@@ -370,12 +368,8 @@ knowledge of the SVG graphics language (see the {ref}`sec_tskit_viz_legend_examp
370368later in this tutorial for a node colour legend).
371369
372370``` {code-cell} ipython3
373- mut_labels = { # Alternative way of defining a label dictionary using a dict comprehension
374- mut.id: (
375- (ts_mutated.mutation(mut.parent).derived_state if mut.parent < 0 else site.ancestral_state) +
376- str(int(site.position)) +
377- mut.derived_state
378- )
371+ mut_labels = {
372+ mut.id: f"{mut.inherited_state}{int(site.position)}{mut.derived_state}"
379373 for site in ts_mutated.sites() for mut in site.mutations
380374}
381375
0 commit comments