我在使用子图显示一些图形时,标签与最后一个子图混在一起,导致图形大小不相等。而且前五个图形并不是完美的圆形。
这是我的代码:
for i in range(6): plt.subplot(231 + i) plt.title("Department " + depts[i]) labels = ['Male', 'Female'] colors = ['#3498DB', '#E74C3C'] sizes = [male_accept_rates[i] / (male_accept_rates[i] + female_accept_rates[i]), female_accept_rates[i] / (male_accept_rates[i] + female_accept_rates[i])] patches, texts = plt.pie(sizes, colors=colors, startangle=90)plt.axis('equal')plt.tight_layout()plt.legend(labels, loc="best")plt.show()
谁能给我一些建议?非常感谢。
回答:
看起来plt.axis('equal')
只对最后一个子图生效。所以你的解决方法是将这行代码放到循环中。
因此:
import numpy as npimport matplotlib.pyplot as pltdepts = 'abcdefg'male_accept_rates = np.array([ 2, 3, 2, 3, 4, 5], float)female_accept_rates= np.array([ 3, 3, 4, 3, 2, 4], float)for i in range(6): plt.subplot(231 + i) plt.title("Department " + depts[i]) labels = ['Male', 'Female'] colors = ['#3498DB', '#E74C3C'] sizes = [male_accept_rates[i] / (male_accept_rates[i] + female_accept_rates[i]), female_accept_rates[i] / (male_accept_rates[i] + female_accept_rates[i])] patches, texts = plt.pie(sizes, colors=colors, startangle=90) plt.axis('equal') plt.tight_layout() plt.legend(labels, loc="best") plt.show()