0

I have a simple pie chart in Python:

values = [3, 5, 12, 8]
labels = ['a', 'b', 'c', 'd']
plt.pie(values, labels)

Which looks something like:

pie chart

I also have a dictionary of values:

dictionary = {'a': 0.31, 'b': 0.11, 'c' : 0.07, 'd': 0.12}

I would like to label each slice with its corresponding value in the dictionary. How do I do that? I read this post which demonstrated how to pass extra arguments to the autopct function, but it seems that the arguments must be the same for each slice, whereas in this case, they are different for each slice.

  • What is the relevance of `values` to the values in the `dictionary`? Are they just integer values to keep the approximate ratios of the floating point values of `dictionary`? – hesham_EE Sep 30 '20 at 23:28
  • They are not related. The values in `dictionary` are a function of the label name. –  Oct 01 '20 at 00:28
  • You said they are not related then accepted a solution that uses the values in the dictionary to draw the pi chart!!! – hesham_EE Oct 01 '20 at 02:41

1 Answers1

0

I understand that what you are looking for is to label each piece with the share of the pie defined as values in the dict, is that right ?

Something like:

dictionary = {'a': 0.31, 'b': 0.11, 'c' : 0.07, 'd': 0.12}
labels = dictionary.keys()
sizes = dictionary.values()
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)
plt.show()

Hope this works for you.

marmurar
  • 116
  • 3
  • I was hoping to have the labels on the slices instead of next to them, but I guess that will do. –  Oct 01 '20 at 00:27