Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I left align a Python Matplotlib pie chart?

I have a pie chart with a left aligned title:

import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
sizes = [15, 30, 45, 10]

fig1, ax1 = plt.subplots()
ax1.pie(sizes, autopct='%1.1f%%', startangle=90)
ax1.axis('equal')
ax1.set_title('Pie Title', loc='left')

plt.tight_layout()
plt.savefig(r'C:\images\pie.png', bbox_inches='tight')

And this code produces this image:

Pie Chart

But there's too much whitespace around the pie chart. How can I left align the title and pie chart and crop the unnecessary whitespace?

Left Align Pie Chart

like image 888
E.Beach Avatar asked Feb 20 '17 14:02

E.Beach


1 Answers

You can start off with a square figure, e.g. figsize=(4,4).
You can then add some whitespaces to have the title aligned.

import matplotlib.pyplot as plt

sizes = [15, 30, 45, 10]

fig1, ax1 = plt.subplots(figsize=(4,4))
ax1.pie(sizes, autopct='%1.1f%%', startangle=90)
ax1.axis('equal')
ax1.set_title('  Pie Title', loc='left')

plt.tight_layout()
plt.savefig(__file__+"pie.png", bbox_inches='tight')
plt.show()

enter image description here

If this isn't enough whitespace clearance, you can play around with subplots_adjust, like plt.subplots_adjust(bottom=0, top=0.93, left=0.0, right=1).

like image 115
ImportanceOfBeingErnest Avatar answered Sep 24 '22 17:09

ImportanceOfBeingErnest