TransWikia.com

ListPlot give special labels position with Callout

Mathematica Asked by zongxian on May 16, 2021

When I use ListPlot, I want to show the labels, such as

ListPlot[Callout[#, #, Above] & /@ Range[10], Joined -> True, Mesh -> All]

Now all positions are Above, but sometimes the labels will over other text, so I want to set some of them Below, for example, the second and third point labels are Below, as the image shows.

enter image description here

2 Answers

ListPlot[Callout[#, #, # /. {2 | 3 | 8 -> Below, _ -> Above}] & /@ Range[10], 
  Joined -> True, Mesh -> All]

enter image description here

Correct answer by kglr on May 16, 2021

One way is use MapAt

data = Range[10];
mapBelow[n_] := MapAt[Callout[#, #, Below] &, n];
mapAbove[n_] := MapAt[Callout[#, #, Above] &, n];
below = Table[mapBelow[n], {n, {2, 3, 8}}];
above = Table[mapAbove[n], {n, Complement[Range[10], {2, 3, 8}]}];
ListPlot[Composition[Sequence @@ Flatten[{above, below}]][data], 
 Joined -> True, Mesh -> All]

enter image description here

Answered by cvgmt on May 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP