0

I am trying to plot a pie chart but not getting the labels at correct position . here is my code

  import pandas as pd
  %matplotlib inline
  import seaborn as sns
  import matplotlib.pyplot as plt
  import numpy as np


labels=Main_df['Rel_Category']
values =  Main_df['Percentage']
explode = (0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
pie = plt.pie(values, labels=labels, explode=explode, shadow=True, 
autopct='%1.1f%%')
plt.legend(pie[0], labels, loc="upper corner")

This is a link to image of pie chart I am getting. Pie chart This is the data frame from where i am taking values

data frame I am new to stackoverflow hence the images are in links.

PriyalChaudhari
  • 363
  • 1
  • 7
  • 23

1 Answers1

2

Are you referring to the legends box instead? You can position the legend by passing the bbox_to_anchor and loc arguments.

labels=Main_df['Rel_Category']
values =  Main_df['Percentage']
explode = (0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
pie = plt.pie(values, labels=labels, explode=explode, shadow=True, 
autopct='%1.1f%%', pctdistance = 1.1, labeldistance = 1.1)
plt.legend(pie[0], labels, loc="upper corner", bbox_to_anchor = (1,1))

Play around with the bbox_to_anchor to shift it around. You can change pctdistance (distance between percentage) and labeldistance (distance between labels) parameter to suit your needs too.

Impuls3H
  • 303
  • 1
  • 2
  • 11
  • This worked well. thank you. !!! @Impuls3H – PriyalChaudhari Apr 21 '17 at 01:09
  • You're welcome! It'll be great if you can select my answer as the correct answer, I'm trying to amass enough reputation to at least comment to get full functionality. Thanks! – Impuls3H Apr 21 '17 at 01:22
  • Also can you guide me about how to show values outside the pie chart . as you can see the smaller values are not showing properly how to solve this issue. – PriyalChaudhari Apr 21 '17 at 01:23
  • You can pass the pctdistance and labeldistance arguments to change the distance between the percentage and the labels. – Impuls3H Apr 21 '17 at 01:30
  • @ Impuls3H That solved the problem. Now my values outside figure. One more question I have is is it possible to give your own values for percentage in pie chart . Becoz for now my pie chart generating percentage values. I want to gives values from my data frame percentage column – PriyalChaudhari Apr 21 '17 at 01:39
  • Are you looking to show the raw data instead of getting the percentage? In that case, pass this `autopct = '%1.1f'`. That will give you the raw data. You can change the string format accordingly. – Impuls3H Apr 21 '17 at 01:54