1

I want to make a pie chart in matplotlib that look like this:

there

(differences in colors and borders aren't a big deal.)

I'm not exactly sure how to make this happen using matplotlib.

Code:

import matplotlib.pyplot as plt

g_title = "Big fat redacted title (Income Distribution)"
g_data_list = [['FIN.RES 47% - $41,888.08', 47], ['VSC 40% - $36,019.00', 40], ['AFTERMARKET 6% - $5,570.00', 6], ['GAP 7% - $6,528.00', 7]]

labels = []
sizes = []
for entry in g_data_list:
    labels.append(entry[0])
    sizes.append(entry[1])

patches, texts = plt.pie(sizes, startangle=90)
plt.legend(patches, labels, loc="center right", fontsize=6)
plt.axis('equal')
plt.title("{}".format(g_title), fontsize=14)
plt.savefig('test.png', dpi=100)

That gives

enter image description here

1 Answer 1

3

I am not sure of what do you like, but one option is to use loc="best"

import matplotlib.pyplot as plt

g_title = "Big fat redacted title (Income Distribution)"
g_data_list = [['FIN.RES 47% - $41,888.08', 47],
               ['VSC 40% - $36,019.00', 40],
               ['AFTERMARKET 6% - $5,570.00', 6],
               ['GAP 7% - $6,528.00', 7]]

labels = []
sizes = []
for entry in g_data_list:
    labels.append(entry[0])
    sizes.append(entry[1])

patches, texts = plt.pie(sizes, startangle=90)
plt.legend(patches, labels, loc="best", fontsize=6)
plt.axis('equal')
plt.title(g_title, fontsize=14)
plt.savefig('test.png', dpi=300)

That gives

enter image description here

Another option is to specify the size of your plot and fix the legend to a location

import matplotlib.pyplot as plt

g_title = "Big fat redacted title (Income Distribution)"
g_data_list = [['FIN.RES 47% - $41,888.08', 47],
               ['VSC 40% - $36,019.00', 40],
               ['AFTERMARKET 6% - $5,570.00', 6],
               ['GAP 7% - $6,528.00', 7]]

labels = []
sizes = []
for entry in g_data_list:
    labels.append(entry[0])
    sizes.append(entry[1])

ax = plt.subplot(111)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.7, box.height])
patches, texts = ax.pie(sizes, startangle=90)
ax.legend(patches, labels, loc='center left',
          bbox_to_anchor=(1, 0.5), fontsize=8)
plt.axis('equal')
plt.suptitle(g_title, fontsize=14)
plt.savefig('test.png', dpi=300)

That results in

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Are you using IPython?
Just running it in pycharm . Yours looks right but now the dimensions are massive - 2,400x1,800. I need 300 x 575.
I have not used PyCharm before, it probably defines the size of the figure automatically. Run it in plain Python. If you need an image with less resolution, just change the parameter dpi from dpi=300 to dpi=100.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.