4
$\begingroup$

I have a PieChart:

PieChart[{{1, 2, 3}, {4, 5, 6}}]

I want to label each sublist differently, but

  PieChart[{{1, 2, 3}, {4, 5, 6}}, ChartLabels -> {{"l1", "l2", "l3"}, {"l4", "l5", "l6"}}]

does not work, because the first part of the Labellist will be associated with the row label. So I thought the only solution to this will be to assign a Label to each entry separately by using Labeled

But then I got stuck with distributing the Labeled function over the values list. For example, given the list of values for the PieChart:

labList = {{1, 2, 3}, {4, 5, 6}}

I can apply Labeled to each element in this list:

labeledStuff = Map[Labeled[#, "Label"] &, labList, {2}]

But how to apply a different label to each element? That is, given the list with labels:

labels = {{"l1", "l2", "l3"}, {"l4", "l5", "l6"}}

apply each of these labels to each element of the value list, resulting in the following:

{{Labeled[1,"l1"],Labeled[2,"l2"],Labeled[3,"l3"]},{Labeled[4,"l4"],Labeled[5,"l5"],Labeled[6,"l6"]}}
$\endgroup$
  • 1
    $\begingroup$ Try PieChart[MapThread[Labeled, {{{1, 2, 3}, {4, 5, 6}}, {{"l1", "l2", "l3"}, {"l4", "l5", "l6"}}}, 2]]. $\endgroup$ – J. M. will be back soon Aug 19 '17 at 17:01
  • $\begingroup$ Aha MapThread is the function I need, god I love mathematica, thanks a bunch ;)! $\endgroup$ – holistic Aug 19 '17 at 17:04
  • $\begingroup$ @J.M: Quick follow-up question: What if I want to supply options to Labeled like Background->Red or LabelStyle->Red or something? I tried PieChart[MapThread[ Labeled[#1, #2, LabelStyle -> Red] &, {{{1, 2, 3}, {4, 5, 6}}, {{"l1", "l2", "l3"}, {"l4", "l5", "l6"}}}, 2]] but get an error message $\endgroup$ – holistic Aug 19 '17 at 18:24
  • 1
    $\begingroup$ LabelStyle won't work in that case, yes. Try PieChart[MapThread[ Labeled[#1, Style[#2, Red]] &, {{{1, 2, 3}, {4, 5, 6}}, {{"l1", "l2", "l3"}, {"l4", "l5", "l6"}}}, 2]]. $\endgroup$ – J. M. will be back soon Aug 19 '17 at 18:28
  • $\begingroup$ I see, thank you again!! $\endgroup$ – holistic Aug 19 '17 at 20:52
4
$\begingroup$
data = {{1, 2, 3}, {4, 5, 6}};
labels = {{"l1", "l2", "l3"}, {"l4", "l5", "l6"}};

PieChart[Thread[Labeled @@ #] & /@ Transpose[{data, labels}]] (* or *)
PieChart[Thread /@ Thread[Labeled[data, labels]]]

enter image description here

$\endgroup$
  • $\begingroup$ Always multiple solutions with mathematica ;)...like that one as well, thanks! $\endgroup$ – holistic Aug 19 '17 at 17:14
  • 1
    $\begingroup$ @holistic, my pleasure. Thank you for the accept. $\endgroup$ – kglr Aug 19 '17 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.