使用具有不同数据框形状的matplotlib绘制条形图

时间:2018-12-26 19:57:39

标签: python pandas numpy matplotlib bar-chart

我有以下三个不同的数据框,前两个的形状为(4,),最后一个的形状为(2,)。如何转换数据框的形状?

当我尝试在条形图中绘制所有三个图形时,最后一个DF失败,并显示“ ValueError:形状不匹配:对象无法广播为单个形状”

如何通过将“ Empty”和“ Invalid”显示为0来在同一条形图中绘制DF3。

DF1:

Authentication auth = SecurityContextHolder.getContext().getAuthentication();



   System.out.println("--------------------------------------------------------------");
    JwtUser jwtUser = (JwtUser) auth.getPrincipal();

    //Get the username of the logged in user: getPrincipal()
    System.out.println("auth.getPrincipal()=>"+jwtUser.getUsername() );
    //Get the password of the authenticated user: getCredentials()
    System.out.println("auth.getCredentials()=>"+auth.getCredentials());
    //Get the assigned roles of the authenticated user: getAuthorities()
    System.out.println("auth.getAuthorities()=>"+auth.getAuthorities());
    //Get further details of the authenticated user: getDetails()
    System.out.println("auth.getDetails()=>"+auth.getDetails());
    System.out.println("--------------------------------------------------------------");

DF2:

Validity

Empty               2672

InValid              581

Multiple Entries     282

Valid               5526

Name: Lifecycle, dtype: int64

DF3:

Validity

Empty                1920

InValid               471

Multiple Entries     2325

Valid               33446

Name: Lifecycle, dtype: int64

下面是我的代码。

Validity

Multiple Entries    10334

Valid               11984

Name: Lifecycle, dtype: int64

1 个答案:

答案 0 :(得分:0)

@busybear在评论中给出了正确的答案。您的代码不可运行。如果我会猜测,可以尝试以下代码:

glot = sample_lot_number.groupby("Validity")
vlot = sample1_lot_number.groupby("Validity")
dplot = Data_Package_Lot_Number.dplot.groupby("Validity")
df1 = glot.Lifecycle.count()
df2 = vlot.Lifecycle.count().reindex(df1)
df3 = dplot.Lifecycle.count().reindex(df1).fillna(0)

ind = np.arange(4)
width = 0.15
ax = plt.subplot()
p1 = ax.bar(ind+width, df1, width)
p2 = ax.bar(ind, df2, width)
p3 = ax.bar(ind-width, df3, width)
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(("Empty","InValid","Multiple Entries","Valid"))