使用 matplotlib subplot 排列饼图

use*_*653 2 python matplotlib subplot

我有 7 个 pi 图表(下面列出了 4 个)。我正在尝试创建一个仪表板,第一行有 4 个饼图,第二行有 3 个饼图。不知道下面的代码哪里出了问题。有没有其他选择来实现这一目标?任何帮助,将不胜感激。

from matplotlib import pyplot as PLT
fig = PLT.figure()
ax1 = fig.add_subplot(221)
line1 = plt.pie(df_14,colors=("g","r"))
plt.title('EventLogs')
ax1 = fig.add_subplot(223)
line2 = plt.pie(df_24,colors=("g","r"))
plt.title('InstalledApp')
ax1 = fig.add_subplot(222)
line3 = plt.pie(df_34,colors=("g","r"))
plt.title('Drive')
ax1 = fig.add_subplot(224)
line4 = plt.pie(df_44,colors=("g","r"))
plt.title('SQL Job')
ax1 = fig.add_subplot(321)
line5 = plt.pie(df_54,colors=("g","r"))
plt.title('Administrators')
ax2 = fig.add_subplot(212)
PLT.show()
Run Code Online (Sandbox Code Playgroud)

has*_*e55 6

我一直使用的一种更好的方法,至少对我来说更直观,是使用subplot2grid....

fig = plt.figure(figsize=(18,10), dpi=1600)
#this line will produce a figure which has 2 row 
#and 4 columns 
#(0, 0) specifies the left upper coordinate of your plot
ax1 = plt.subplot2grid((2,4),(0,0))
plt.pie(df_14,colors=("g","r"))
plt.title('EventLogs')
#next one
ax1 = plt.subplot2grid((2, 4), (0, 1))
plt.pie(df_24,colors=("g","r"))
plt.title('InstalledApp')
Run Code Online (Sandbox Code Playgroud)

你可以这样继续,当你想切换行时,只需将坐标写为 (1, 0)... 即第二行第一列。

2 行和 2 列的示例 -

fig = plt.figure(figsize=(18,10), dpi=1600)
#2 rows 2 columns

#first row, first column
ax1 = plt.subplot2grid((2,2),(0,0))
plt.pie(df.a,colors=("g","r"))
plt.title('EventLogs')

#first row sec column
ax1 = plt.subplot2grid((2,2), (0, 1))
plt.pie(df.a,colors=("g","r"))
plt.title('EventLog_2')

#Second row first column
ax1 = plt.subplot2grid((2,2), (1, 0))
plt.pie(df.a,colors=("g","r"))
plt.title('InstalledApp')

#second row second column
ax1 = plt.subplot2grid((2,2), (1, 1))
plt.pie(df.a,colors=("g","r"))
plt.title('InstalledApp_2')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

希望这可以帮助!