使用matplotlib subplot排列饼图
Arrangement of pie charts using matplotlib subplot
我有 7 个圆周率图(下面列出了 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()
一个我经常使用并且更直观的更好的方法,至少对我来说,是使用 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')
你可以这样继续下去,当你想切换行时,只需将坐标写为 (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')
希望对您有所帮助!
如果您想更快地安排子图,请使用此选项
除了hashcode55的代码:
当你想避免创建多个 DataFrame 时,我建议将整数分配给你的特征列并遍历它们。不过,请确保为这些功能制作字典。
在这里,我正在绘制一个 4 列和 2 行的图。
fig = plt.figure(figsize=(25,10)) #,dpi=1600)
i= 0 #this is the feature I used
r,c = 0 ,0 #these are the rows(r) and columns(c)
for i in range(7):
if c < 4:
#weekday
ax1 = plt.subplot2grid((2,4), (r, c))
plt.pie(data[data.feature == i].something , labels = ..., autopct='%.0f%%')
plt.title(feature[i])
c +=1 #go one column to the left
i+=1 #go to the next feature
else:
c = 0 #reset column number as we exceeded 4 columns
r = 1 #go into the second row
ax1 = plt.subplot2grid((2,4), (r, c))
plt.pie(data[data.feature == i].something , labels = ..., autopct='%.0f%%')
plt.title(days[i])
c +=1
i+=1
plt.show()
此代码将继续执行,直到特征量用完。
我有 7 个圆周率图(下面列出了 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()
一个我经常使用并且更直观的更好的方法,至少对我来说,是使用 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')
你可以这样继续下去,当你想切换行时,只需将坐标写为 (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')
希望对您有所帮助!
如果您想更快地安排子图,请使用此选项
除了hashcode55的代码:
当你想避免创建多个 DataFrame 时,我建议将整数分配给你的特征列并遍历它们。不过,请确保为这些功能制作字典。 在这里,我正在绘制一个 4 列和 2 行的图。
fig = plt.figure(figsize=(25,10)) #,dpi=1600)
i= 0 #this is the feature I used
r,c = 0 ,0 #these are the rows(r) and columns(c)
for i in range(7):
if c < 4:
#weekday
ax1 = plt.subplot2grid((2,4), (r, c))
plt.pie(data[data.feature == i].something , labels = ..., autopct='%.0f%%')
plt.title(feature[i])
c +=1 #go one column to the left
i+=1 #go to the next feature
else:
c = 0 #reset column number as we exceeded 4 columns
r = 1 #go into the second row
ax1 = plt.subplot2grid((2,4), (r, c))
plt.pie(data[data.feature == i].something , labels = ..., autopct='%.0f%%')
plt.title(days[i])
c +=1
i+=1
plt.show()
此代码将继续执行,直到特征量用完。