Matplotlib - Move labels into middle of pie chart
up vote
6
down vote
favorite
I've got my pie chart working but I noticed that the text boxes for the actual chart doesn't seem to be working correctly. They are just clustered so I was wondering is there any way for me to move the labels into the middle where the white circle is and have the matching colour beside it or not?
crimeTypes = dict(crimeData["Crime type"].value_counts())
crimeType =
totalAmount =
numberOfCrimes = 14
for key in sorted(crimeTypes, key=crimeTypes.get, reverse=True):
crimeType.append(key)
totalAmount.append(crimeTypes.get(key))
crimeType_sample = crimeType[0:numberOfCrimes]
totalAmount_sample = totalAmount[0:numberOfCrimes]
fig1, ax1 = plt.subplots()
ax1.pie(totalAmount_sample, labels=crimeType_sample, autopct='%1.1f%%', shadow=False, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
fig1 = plt.gcf()
fig1.set_size_inches(10,10)
circle = plt.Circle(xy=(0,0), radius=0.75, facecolor='white')
plt.gca().add_artist(circle)
plt.show();
python pandas matplotlib pie-chart
New contributor
add a comment |
up vote
6
down vote
favorite
I've got my pie chart working but I noticed that the text boxes for the actual chart doesn't seem to be working correctly. They are just clustered so I was wondering is there any way for me to move the labels into the middle where the white circle is and have the matching colour beside it or not?
crimeTypes = dict(crimeData["Crime type"].value_counts())
crimeType =
totalAmount =
numberOfCrimes = 14
for key in sorted(crimeTypes, key=crimeTypes.get, reverse=True):
crimeType.append(key)
totalAmount.append(crimeTypes.get(key))
crimeType_sample = crimeType[0:numberOfCrimes]
totalAmount_sample = totalAmount[0:numberOfCrimes]
fig1, ax1 = plt.subplots()
ax1.pie(totalAmount_sample, labels=crimeType_sample, autopct='%1.1f%%', shadow=False, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
fig1 = plt.gcf()
fig1.set_size_inches(10,10)
circle = plt.Circle(xy=(0,0), radius=0.75, facecolor='white')
plt.gca().add_artist(circle)
plt.show();
python pandas matplotlib pie-chart
New contributor
I'm gonna drop pandas from the title because it's a matplotlib issue, but nice question :)
– roganjosh
Nov 10 at 14:07
No worries, sorry about that, wasn't too sure which thing I had to add!
– ComSci Student
Nov 10 at 14:09
3
Don't be sorry, it's a refreshing question from a new user. I have a feeling I know who can/will answer this (I can't) but if you can include data to make a repeatable example for them to test with, even better. Doesn't have to be your real data, just a minimal example.
– roganjosh
Nov 10 at 14:12
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I've got my pie chart working but I noticed that the text boxes for the actual chart doesn't seem to be working correctly. They are just clustered so I was wondering is there any way for me to move the labels into the middle where the white circle is and have the matching colour beside it or not?
crimeTypes = dict(crimeData["Crime type"].value_counts())
crimeType =
totalAmount =
numberOfCrimes = 14
for key in sorted(crimeTypes, key=crimeTypes.get, reverse=True):
crimeType.append(key)
totalAmount.append(crimeTypes.get(key))
crimeType_sample = crimeType[0:numberOfCrimes]
totalAmount_sample = totalAmount[0:numberOfCrimes]
fig1, ax1 = plt.subplots()
ax1.pie(totalAmount_sample, labels=crimeType_sample, autopct='%1.1f%%', shadow=False, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
fig1 = plt.gcf()
fig1.set_size_inches(10,10)
circle = plt.Circle(xy=(0,0), radius=0.75, facecolor='white')
plt.gca().add_artist(circle)
plt.show();
python pandas matplotlib pie-chart
New contributor
I've got my pie chart working but I noticed that the text boxes for the actual chart doesn't seem to be working correctly. They are just clustered so I was wondering is there any way for me to move the labels into the middle where the white circle is and have the matching colour beside it or not?
crimeTypes = dict(crimeData["Crime type"].value_counts())
crimeType =
totalAmount =
numberOfCrimes = 14
for key in sorted(crimeTypes, key=crimeTypes.get, reverse=True):
crimeType.append(key)
totalAmount.append(crimeTypes.get(key))
crimeType_sample = crimeType[0:numberOfCrimes]
totalAmount_sample = totalAmount[0:numberOfCrimes]
fig1, ax1 = plt.subplots()
ax1.pie(totalAmount_sample, labels=crimeType_sample, autopct='%1.1f%%', shadow=False, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
fig1 = plt.gcf()
fig1.set_size_inches(10,10)
circle = plt.Circle(xy=(0,0), radius=0.75, facecolor='white')
plt.gca().add_artist(circle)
plt.show();
python pandas matplotlib pie-chart
python pandas matplotlib pie-chart
New contributor
New contributor
edited Nov 10 at 14:09
New contributor
asked Nov 10 at 14:03
ComSci Student
433
433
New contributor
New contributor
I'm gonna drop pandas from the title because it's a matplotlib issue, but nice question :)
– roganjosh
Nov 10 at 14:07
No worries, sorry about that, wasn't too sure which thing I had to add!
– ComSci Student
Nov 10 at 14:09
3
Don't be sorry, it's a refreshing question from a new user. I have a feeling I know who can/will answer this (I can't) but if you can include data to make a repeatable example for them to test with, even better. Doesn't have to be your real data, just a minimal example.
– roganjosh
Nov 10 at 14:12
add a comment |
I'm gonna drop pandas from the title because it's a matplotlib issue, but nice question :)
– roganjosh
Nov 10 at 14:07
No worries, sorry about that, wasn't too sure which thing I had to add!
– ComSci Student
Nov 10 at 14:09
3
Don't be sorry, it's a refreshing question from a new user. I have a feeling I know who can/will answer this (I can't) but if you can include data to make a repeatable example for them to test with, even better. Doesn't have to be your real data, just a minimal example.
– roganjosh
Nov 10 at 14:12
I'm gonna drop pandas from the title because it's a matplotlib issue, but nice question :)
– roganjosh
Nov 10 at 14:07
I'm gonna drop pandas from the title because it's a matplotlib issue, but nice question :)
– roganjosh
Nov 10 at 14:07
No worries, sorry about that, wasn't too sure which thing I had to add!
– ComSci Student
Nov 10 at 14:09
No worries, sorry about that, wasn't too sure which thing I had to add!
– ComSci Student
Nov 10 at 14:09
3
3
Don't be sorry, it's a refreshing question from a new user. I have a feeling I know who can/will answer this (I can't) but if you can include data to make a repeatable example for them to test with, even better. Doesn't have to be your real data, just a minimal example.
– roganjosh
Nov 10 at 14:12
Don't be sorry, it's a refreshing question from a new user. I have a feeling I know who can/will answer this (I can't) but if you can include data to make a repeatable example for them to test with, even better. Doesn't have to be your real data, just a minimal example.
– roganjosh
Nov 10 at 14:12
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Here's some sample data to reproduce your problem:
Sample Data:
import pandas as pd
data = (['Burglary']*50 + ['Arson', 'Theft', 'Violence'] + ['Drugs']*10 + ['Other'] +
['Shoplifting']*4 + ['Harassment']*17 + ['Murder', 'Vehicle Crime']*3 +
['Some other Crimes']*12 + ['Even More Crime', 'And Crime', 'And More Crime']*10)
crimeData = pd.DataFrame(data, columns=['Crime type'])
Which will result in this plot:
Use a legend
Do not plot the percentages or labels when you plot, and then create a legend which is placed off to the side:
fig1, ax1 = plt.subplots()
ax1.pie(totalAmount_sample, shadow=False, startangle=90) # No labels or %s
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
fig1 = plt.gcf()
fig1.set_size_inches(5,5)
circle = plt.Circle(xy=(0,0), radius=0.75, facecolor='white')
plt.gca().add_artist(circle)
plt.legend(labels=[f'x np.round(y/sum(totalAmount_sample)*100,1)%' for x,y in crimeTypes.items()],
bbox_to_anchor=(1,1))
plt.show();
Rotate the labels:
Create your labels and use rotatelabels=True
. Though this may still appear cramped in many cases.
fig1, ax1 = plt.subplots()
labels=[f'x np.round(y/sum(totalAmount_sample)*100,1)%' for x,y in crimeTypes.items()]
ax1.pie(totalAmount_sample, labels=labels, shadow=False, startangle=90,
rotatelabels=True) # No %
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
fig1 = plt.gcf()
fig1.set_size_inches(7,7)
circle = plt.Circle(xy=(0,0), radius=0.75, facecolor='white')
plt.gca().add_artist(circle)
plt.show();
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Here's some sample data to reproduce your problem:
Sample Data:
import pandas as pd
data = (['Burglary']*50 + ['Arson', 'Theft', 'Violence'] + ['Drugs']*10 + ['Other'] +
['Shoplifting']*4 + ['Harassment']*17 + ['Murder', 'Vehicle Crime']*3 +
['Some other Crimes']*12 + ['Even More Crime', 'And Crime', 'And More Crime']*10)
crimeData = pd.DataFrame(data, columns=['Crime type'])
Which will result in this plot:
Use a legend
Do not plot the percentages or labels when you plot, and then create a legend which is placed off to the side:
fig1, ax1 = plt.subplots()
ax1.pie(totalAmount_sample, shadow=False, startangle=90) # No labels or %s
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
fig1 = plt.gcf()
fig1.set_size_inches(5,5)
circle = plt.Circle(xy=(0,0), radius=0.75, facecolor='white')
plt.gca().add_artist(circle)
plt.legend(labels=[f'x np.round(y/sum(totalAmount_sample)*100,1)%' for x,y in crimeTypes.items()],
bbox_to_anchor=(1,1))
plt.show();
Rotate the labels:
Create your labels and use rotatelabels=True
. Though this may still appear cramped in many cases.
fig1, ax1 = plt.subplots()
labels=[f'x np.round(y/sum(totalAmount_sample)*100,1)%' for x,y in crimeTypes.items()]
ax1.pie(totalAmount_sample, labels=labels, shadow=False, startangle=90,
rotatelabels=True) # No %
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
fig1 = plt.gcf()
fig1.set_size_inches(7,7)
circle = plt.Circle(xy=(0,0), radius=0.75, facecolor='white')
plt.gca().add_artist(circle)
plt.show();
add a comment |
up vote
2
down vote
accepted
Here's some sample data to reproduce your problem:
Sample Data:
import pandas as pd
data = (['Burglary']*50 + ['Arson', 'Theft', 'Violence'] + ['Drugs']*10 + ['Other'] +
['Shoplifting']*4 + ['Harassment']*17 + ['Murder', 'Vehicle Crime']*3 +
['Some other Crimes']*12 + ['Even More Crime', 'And Crime', 'And More Crime']*10)
crimeData = pd.DataFrame(data, columns=['Crime type'])
Which will result in this plot:
Use a legend
Do not plot the percentages or labels when you plot, and then create a legend which is placed off to the side:
fig1, ax1 = plt.subplots()
ax1.pie(totalAmount_sample, shadow=False, startangle=90) # No labels or %s
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
fig1 = plt.gcf()
fig1.set_size_inches(5,5)
circle = plt.Circle(xy=(0,0), radius=0.75, facecolor='white')
plt.gca().add_artist(circle)
plt.legend(labels=[f'x np.round(y/sum(totalAmount_sample)*100,1)%' for x,y in crimeTypes.items()],
bbox_to_anchor=(1,1))
plt.show();
Rotate the labels:
Create your labels and use rotatelabels=True
. Though this may still appear cramped in many cases.
fig1, ax1 = plt.subplots()
labels=[f'x np.round(y/sum(totalAmount_sample)*100,1)%' for x,y in crimeTypes.items()]
ax1.pie(totalAmount_sample, labels=labels, shadow=False, startangle=90,
rotatelabels=True) # No %
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
fig1 = plt.gcf()
fig1.set_size_inches(7,7)
circle = plt.Circle(xy=(0,0), radius=0.75, facecolor='white')
plt.gca().add_artist(circle)
plt.show();
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Here's some sample data to reproduce your problem:
Sample Data:
import pandas as pd
data = (['Burglary']*50 + ['Arson', 'Theft', 'Violence'] + ['Drugs']*10 + ['Other'] +
['Shoplifting']*4 + ['Harassment']*17 + ['Murder', 'Vehicle Crime']*3 +
['Some other Crimes']*12 + ['Even More Crime', 'And Crime', 'And More Crime']*10)
crimeData = pd.DataFrame(data, columns=['Crime type'])
Which will result in this plot:
Use a legend
Do not plot the percentages or labels when you plot, and then create a legend which is placed off to the side:
fig1, ax1 = plt.subplots()
ax1.pie(totalAmount_sample, shadow=False, startangle=90) # No labels or %s
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
fig1 = plt.gcf()
fig1.set_size_inches(5,5)
circle = plt.Circle(xy=(0,0), radius=0.75, facecolor='white')
plt.gca().add_artist(circle)
plt.legend(labels=[f'x np.round(y/sum(totalAmount_sample)*100,1)%' for x,y in crimeTypes.items()],
bbox_to_anchor=(1,1))
plt.show();
Rotate the labels:
Create your labels and use rotatelabels=True
. Though this may still appear cramped in many cases.
fig1, ax1 = plt.subplots()
labels=[f'x np.round(y/sum(totalAmount_sample)*100,1)%' for x,y in crimeTypes.items()]
ax1.pie(totalAmount_sample, labels=labels, shadow=False, startangle=90,
rotatelabels=True) # No %
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
fig1 = plt.gcf()
fig1.set_size_inches(7,7)
circle = plt.Circle(xy=(0,0), radius=0.75, facecolor='white')
plt.gca().add_artist(circle)
plt.show();
Here's some sample data to reproduce your problem:
Sample Data:
import pandas as pd
data = (['Burglary']*50 + ['Arson', 'Theft', 'Violence'] + ['Drugs']*10 + ['Other'] +
['Shoplifting']*4 + ['Harassment']*17 + ['Murder', 'Vehicle Crime']*3 +
['Some other Crimes']*12 + ['Even More Crime', 'And Crime', 'And More Crime']*10)
crimeData = pd.DataFrame(data, columns=['Crime type'])
Which will result in this plot:
Use a legend
Do not plot the percentages or labels when you plot, and then create a legend which is placed off to the side:
fig1, ax1 = plt.subplots()
ax1.pie(totalAmount_sample, shadow=False, startangle=90) # No labels or %s
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
fig1 = plt.gcf()
fig1.set_size_inches(5,5)
circle = plt.Circle(xy=(0,0), radius=0.75, facecolor='white')
plt.gca().add_artist(circle)
plt.legend(labels=[f'x np.round(y/sum(totalAmount_sample)*100,1)%' for x,y in crimeTypes.items()],
bbox_to_anchor=(1,1))
plt.show();
Rotate the labels:
Create your labels and use rotatelabels=True
. Though this may still appear cramped in many cases.
fig1, ax1 = plt.subplots()
labels=[f'x np.round(y/sum(totalAmount_sample)*100,1)%' for x,y in crimeTypes.items()]
ax1.pie(totalAmount_sample, labels=labels, shadow=False, startangle=90,
rotatelabels=True) # No %
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
fig1 = plt.gcf()
fig1.set_size_inches(7,7)
circle = plt.Circle(xy=(0,0), radius=0.75, facecolor='white')
plt.gca().add_artist(circle)
plt.show();
answered Nov 10 at 17:03
ALollz
9,92031134
9,92031134
add a comment |
add a comment |
ComSci Student is a new contributor. Be nice, and check out our Code of Conduct.
ComSci Student is a new contributor. Be nice, and check out our Code of Conduct.
ComSci Student is a new contributor. Be nice, and check out our Code of Conduct.
ComSci Student is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239733%2fmatplotlib-move-labels-into-middle-of-pie-chart%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm gonna drop pandas from the title because it's a matplotlib issue, but nice question :)
– roganjosh
Nov 10 at 14:07
No worries, sorry about that, wasn't too sure which thing I had to add!
– ComSci Student
Nov 10 at 14:09
3
Don't be sorry, it's a refreshing question from a new user. I have a feeling I know who can/will answer this (I can't) but if you can include data to make a repeatable example for them to test with, even better. Doesn't have to be your real data, just a minimal example.
– roganjosh
Nov 10 at 14:12