如何在 Python 中显示 2 行 2 列的饼图
How do I show pie graph in 2 row and 2 column in Python
我在以 2 行 2 列显示饼图时遇到问题。它们列在一栏中。
我该如何解决这个问题?
下面是我的代码片段。
plt.figure(figsize = (8,8))
ax1 = plt.subplot(2,2,1)
coursera_df_beginner["course_Certificate_type"].value_counts().plot(kind='pie',shadow=True, explode=(0.1, 0, 0), startangle=90,autopct='%1.1f%%', ax=ax1)
plt.title('Difficulty in Courses')
plt.ylabel("")
plt.figure(figsize = (8,8))
ax2 = plt.subplot(2,2,2)
coursera_df_intermediate["course_Certificate_type"].value_counts().plot(kind='pie',shadow=True, explode=(0.1, 0, 0), startangle=90,autopct='%1.1f%%', ax=ax2)
plt.title('Difficulty in Courses')
plt.ylabel("")
plt.figure(figsize = (8,8))
ax3 = plt.subplot(2,2,3)
coursera_df_mixed["course_Certificate_type"].value_counts().plot(kind='pie',shadow=True, explode=(0.1,), startangle=90,autopct='%1.1f%%', ax=ax3)
plt.title('Difficulty in Courses')
plt.ylabel("")
plt.figure(figsize = (8,8))
ax4 = plt.subplot(2,2,4)
coursera_df_advanced["course_Certificate_type"].value_counts().plot(kind='pie',shadow=True, explode=(0.1, 0), startangle=90,autopct='%1.1f%%', ax=ax4)
plt.title('Difficulty in Courses')
plt.ylabel("")
这是我的答案
f,a = plt.subplots(2,2,figsize=(8,8))
f.subplots_adjust(wspace = .8)
coursera_df_beginner["course_Certificate_type"].value_counts().plot(kind='pie',
shadow=True,
explode=(0.1, 0, 0),
startangle=90,
autopct='%1.1f%%', ax=a[0,0])
a[0,0].set_title('Beginner Course')
a[0,0].set_ylabel('');
coursera_df_intermediate["course_Certificate_type"].value_counts().plot(kind='pie',
shadow=True,
explode=(0.1, 0, 0),
startangle=90,
autopct='%1.1f%%', ax=a[0,1])
a[0,1].set_title('Intermediate Course')
a[0,1].set_ylabel('');
coursera_df_mixed["course_Certificate_type"].value_counts().plot(kind='pie',
shadow=True,
explode=(0.1,),
startangle=90,
autopct='%1.1f%%',
ax=a[1,0])
a[1,0].set_title('Mixed Course')
a[1,0].set_ylabel('');
coursera_df_advanced["course_Certificate_type"].value_counts().plot(kind='pie',
shadow=True,
explode=(0.1, 0),
startangle=90,
autopct='%1.1f%%',
ax=a[1,1])
a[1,1].set_title('Advanced Course')
a[1,1].set_ylabel('');
我在以 2 行 2 列显示饼图时遇到问题。它们列在一栏中。
我该如何解决这个问题?
下面是我的代码片段。
plt.figure(figsize = (8,8))
ax1 = plt.subplot(2,2,1)
coursera_df_beginner["course_Certificate_type"].value_counts().plot(kind='pie',shadow=True, explode=(0.1, 0, 0), startangle=90,autopct='%1.1f%%', ax=ax1)
plt.title('Difficulty in Courses')
plt.ylabel("")
plt.figure(figsize = (8,8))
ax2 = plt.subplot(2,2,2)
coursera_df_intermediate["course_Certificate_type"].value_counts().plot(kind='pie',shadow=True, explode=(0.1, 0, 0), startangle=90,autopct='%1.1f%%', ax=ax2)
plt.title('Difficulty in Courses')
plt.ylabel("")
plt.figure(figsize = (8,8))
ax3 = plt.subplot(2,2,3)
coursera_df_mixed["course_Certificate_type"].value_counts().plot(kind='pie',shadow=True, explode=(0.1,), startangle=90,autopct='%1.1f%%', ax=ax3)
plt.title('Difficulty in Courses')
plt.ylabel("")
plt.figure(figsize = (8,8))
ax4 = plt.subplot(2,2,4)
coursera_df_advanced["course_Certificate_type"].value_counts().plot(kind='pie',shadow=True, explode=(0.1, 0), startangle=90,autopct='%1.1f%%', ax=ax4)
plt.title('Difficulty in Courses')
plt.ylabel("")
这是我的答案
f,a = plt.subplots(2,2,figsize=(8,8))
f.subplots_adjust(wspace = .8)
coursera_df_beginner["course_Certificate_type"].value_counts().plot(kind='pie',
shadow=True,
explode=(0.1, 0, 0),
startangle=90,
autopct='%1.1f%%', ax=a[0,0])
a[0,0].set_title('Beginner Course')
a[0,0].set_ylabel('');
coursera_df_intermediate["course_Certificate_type"].value_counts().plot(kind='pie',
shadow=True,
explode=(0.1, 0, 0),
startangle=90,
autopct='%1.1f%%', ax=a[0,1])
a[0,1].set_title('Intermediate Course')
a[0,1].set_ylabel('');
coursera_df_mixed["course_Certificate_type"].value_counts().plot(kind='pie',
shadow=True,
explode=(0.1,),
startangle=90,
autopct='%1.1f%%',
ax=a[1,0])
a[1,0].set_title('Mixed Course')
a[1,0].set_ylabel('');
coursera_df_advanced["course_Certificate_type"].value_counts().plot(kind='pie',
shadow=True,
explode=(0.1, 0),
startangle=90,
autopct='%1.1f%%',
ax=a[1,1])
a[1,1].set_title('Advanced Course')
a[1,1].set_ylabel('');