0

Sorry for posting mistake.I'm new here. how can i add the numbers of data beside the legend table? Also How can I make the legend table on the side so as not to block the visibility of the chart?

Example legend table:

(redcolor) at=1
(bluecolor) nt=3
(purplecolor) ct=5

my code:

aaa=[1,3,5]
name='at','nt','ct'
plt.pie(aaa,labels=name)
plt.legend()
plt.show()
  • See [Legend overlaps with the pie chart](https://stackoverflow.com/questions/43272206/python-legend-overlaps-with-the-pie-chart) – JohanC Apr 01 '21 at 22:42

1 Answers1

0

The comment by JohanC answers your first question. To answer your second question: if you want to add the value to the label, you should create a list of labels which have the values 'baked in.' Essentially, you want to create a list with the strings ['at = 1', 'nt = 3', 'ct = 5']. You can do this as follows using a combination of list comprehension, the zip() function, and string formatting (I am using f-strings in this example):

aaa = [1, 3, 5]
name = ['at', 'nt', 'ct']
labels = [f'{n} = {v}' for n, v in zip(name, aaa)]

You can then feed this list to your plt.pie call:

plt.pie(aaa, labels=labels)
luuk
  • 1,630
  • 4
  • 12