Krante
Krante

Reputation: 43

Subplot Multiple Columns in Pandas Python

I am new to Python and struggling to solve this one efficiently. I read a number of examples but they were complex and lack of understanding. For the below dataframe, I like to subplot per columns while ignoring the first two i.e Site_ID and Cell_ID:

Each subplot (Availability etc..), will include the "Grouped" Site_ID as legends. Each subplot is saved to a desired location.

Sample Data:

Date   Site_ID  Cell_ID   Availability  VoLTE CSSR   VoLTE Attempts
22/03/2019  23181   23181B11    100      99.546435  264
03/03/2019  91219   91219A11    100      99.973934  663
17/04/2019  61212   61212A80    100      99.898843  1289
29/04/2019  91219   91219B26    99.907407   100 147
24/03/2019  61212   61212A11    100      99.831425  812
25/04/2019  61212   61212B11    100      99.91107   2677
29/03/2019  91219   91219A26    100      99.980066  1087
05/04/2019  91705   91705C11    100      99.331263  1090
04/04/2019  91219   91219A26    100      99.984588  914
19/03/2019  61212   61212B11    94.21875    99.934376   2318
23/03/2019  23182   23182B11    100      99.47367   195
02/04/2019  91219   91219A26    100      99.980123  958
26/03/2019  23181   23181A11    100      99.48185   543
19/03/2019  61212   61212A11    94.21875    99.777605   1596
18/04/2019  23182   23182B11    100      99.978012  264
26/03/2019  23181   23181C11    100      99.829911  1347
01/03/2019  91219   91219A11    100      99.770661  1499
12/03/2019  91219   91219B11    100      99.832273  1397
19/04/2019  61212   61212B80    100      99.987946  430
12/03/2019  91705   91705C11    100      98.789819  1000

Here is my inefficient solution and given there are over 100 columns, I am quite worried.

#seperates dataframes
Avail = new_df.loc[:,["Site_ID","Cell_ID","Availability"]]
V_CSSR = new_df.loc[:,["Site_ID","Cell_ID","VoLTE CSSR"]]
V_Atte = new_df.loc[:,["Site_ID","Cell_ID","VoLTE Attempts"]]

#plot each dataframe
Avail.groupby("Site_ID")["Availability"].plot(y="Availability", legend = True) 
V_CSSR.groupby("Site_ID")["VoLTE CSSR"].plot(y="VoLTE CSSR", legend = True)
V_Atte.groupby("Site_ID")["VoLTE Attempts"].plot(y="VoLTE Attempts", legend = True)

This is the outcome I am after. End Result

Upvotes: 0

Views: 3967

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

Not the best solution, but you can try:

fig, axes = plt.subplots(1,3, figsize=(10,4))

for col, ax in zip(cols, axes):
    for site in df.Site_ID.unique():
        tmp_df = df[df.Site_ID.eq(site)]
        ax.plot(tmp_df.Date, tmp_df[col], label=site)

    ax.set_title(col) 
    ax.legend()

plt.show()

Output:

enter image description here

Upvotes: 1

Related Questions