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:
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?
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()
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With