首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用matplotlib subplot排列饼图

使用matplotlib subplot排列饼图
EN

Stack Overflow用户
提问于 2016-07-18 21:38:40
回答 2查看 9.6K关注 0票数 2

我有7张圆周率图(下面列出了4张)。我正在尝试创建一个仪表板与4个饼图在第一行和3个饼图在第二行。不确定我在以下代码中哪里出错了。有没有其他方法可以实现这一点?任何帮助都将不胜感激。

代码语言:javascript
复制
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()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-18 21:52:33

我经常使用的一种更好的方法是使用subplot2grid,它更直观,至少对我来说是这样。

代码语言:javascript
复制
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个cols的示例-

代码语言:javascript
复制
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')

希望这能有所帮助!

票数 6
EN

Stack Overflow用户

发布于 2021-08-17 10:30:52

如果您想要更快地创建子图排列,请使用此选项

除了hashcode55的代码:

当你想要避免创建多个特征时,我建议将整数赋值给你的DataFrames -column并遍历这些整数。不过,要确保你为这些特性建立了一个字典。在这里,我正在做一个4列2行的绘图。

代码语言:javascript
复制
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()

这段代码将一直持续到功能用完为止。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38438220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档