【问题标题】:Arrangement of pie charts using matplotlib subplot使用 matplotlib 子图排列饼图
【发布时间】:2016-07-18 13:38:40
【问题描述】:

我有 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()

【问题讨论】:

    标签: python matplotlib subplot


    【解决方案1】:

    至少对我来说,我一直使用的更好的方法是使用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')
    

    希望这会有所帮助!

    【讨论】:

    • 谢谢,它工作得很好。有没有办法为所有 7 个情节设置一个共同的图例?例如,红色表示“不健康”,绿色表示“健康”?
    • 您正在寻找figlegend,但由于每个地块都有自己的轴,因此无法直接使用legend 来完成。或者为什么不把你的传奇放在一个情节上?
    • 我试过了,但没有按预期工作:lgd = plt.legend((line1), ('Helathy','Not Healthy'), loc='upper right')
    • 这个答案和subplot2grid,值得更多关注。对于这样一个基本任务,Matplotlib 中的子图是我必须处理的最令人沮丧的事情之一。书签以备将来使用!
    【解决方案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()
    

    此代码将继续运行,直到用完所有功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      相关资源
      最近更新 更多