0

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:

  • Availability
  • VoLTE CSSR
  • VoLTE Attempts

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

4
  • you can initiate an axis object, like fig, ax = plt.subplots() and pass the ax=ax into groupby().plot(ax=ax). Commented Jun 12, 2019 at 17:29
  • Like this? fig, ax1 = plt.subplots(figsize = (15,10)) new_df.groupby("Cell_ID").plot(ax=ax1, legend=True) It doesn't give me the desired result. Can you please provide a sample code? Commented Jun 12, 2019 at 18:47
  • Like that. I don’t want to provide sample code as there’s no sample data. Commented Jun 12, 2019 at 19:24
  • I have managed to include the sample data. I hope you can give me some pointers. Anything at all will be much appreciated. Thank you! Commented Jun 13, 2019 at 20:53

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

More or less what I am looking for. I will go through the code to make sense of it and adjust it for my need. Thank you.
where is cols defined
@GoldenLion list of columns to be plotted. In this example, it's ['Availability', 'VoLTE CSSR', 'VoLTE Attempts']. On the other hand, please note that this is a very old answer. A better solution would be sns.FacetGrid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.