novastar
novastar

Reputation: 166

How to add multiple labels on x-axis of groupby plot of pandas dataframe

I have an Excel sheet shared here: https://docs.google.com/spreadsheets/d/1WolE-TpyEXtv1rlr3xusESke46UMlRurAEH1D1Lsyss/edit?usp=sharing

I am trying to create work progress graph and here is my code:

import datetime
from datetime import date
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

df = pd.read_excel('Test Execution Progress.xlsx')

dp = df.groupby(by=['Project ID', 'Release Name', 'Cycle Name'],
                  as_index=False).plot(x=['Test Execution Date'],
                                       y=['Planned', 'Commulative Tested', 'Commulative Passed'],
                                       figsize=(16, 6), linestyle='dashed',marker='D', markersize=5,
                                       title='Trest Exection Progress', legend=True, colormap='jet')

The target is to:

  1. Group the dataframe by Project ID, Release Name and Cycle Name
  2. For Each group draw a plot with Test Execution Date on x-axis and "Cumulative Tested" and "Cumulative Passed" on y-axis showing the progress of test cases execution for each group.
  3. Label x-axis with the respective group of Project ID, Release Name and Cycle Name
  4. label y-axis with the text, "Count of Test Cases"

Help in this regard is highly appreciated.

Edit I found an example from here: enter link description here

And after making some changes, I managed to almost get what I wanted as follows: enter image description here

Now, I just need to:

  1. Remove the parenthesis from the tuples on the top of each plot (example: ('BIC', 'R5', 'C5') on the top of the first plot) which I think that I might be able to do myself.
  2. Remove the duplicate yticks on the left of each of the plot. I don't have any idea for this.

Upvotes: 1

Views: 3178

Answers (1)

novastar
novastar

Reputation: 166

Finally, I resolved all issues and managed to get an desired output. Here is the code:

grouped = dfm.groupby(by=['Project ID', 'Release Name', 'Cycle Name'], as_index=False)
# Calculate the height of table to be displayed above the Testing Progress charts
l = len(dfp['Cycle Name']) + 2

# Create a row_num variable for locating the charts in Excel sheet
row_num = l

# Create a for loop to create charts grouped 
for name, group in grouped:
    image_data = BytesIO()
    fig = plt.figure(figsize=(12, 4), dpi=None, facecolor='white')    
    ax1 = fig.add_subplot(111, facecolor='aliceblue')
    group.plot.line(ax=ax1, color='purple', x='Test Execution Date',
                    y='Planned', linestyle='dashdot', marker='o', markersize=5)
    ax1.set_ylabel('Test Cases Count') 
    ax2 = ax1.twinx()
    group.plot.line(ax=ax1,color='blue',x='Test Execution Date',
                    y='Commulative Tested', linestyle='dotted', marker='o', markersize=5)
#     ax2.set_ylabel('Test Cases Count')
    group.plot.line(ax=ax1, color='green', x='Test Execution Date',
                    y='Commulative Passed', linestyle='dashed' ,marker='o',markersize=5)
    fig.suptitle('Overall Release Progress', fontsize=12, fontweight='bold', x=0.5, y =1.01)
    plt.title(' => '.join([str(i) for i in name]), fontsize=10, loc='center')
    ax1.grid(b=True, which='major', color='grey', linestyle='dotted')
    plt.yticks([])
    ax1.invert_yaxis()
    fig.savefig( image_data, format="png", dpi=100, facecolor='aliceblue', bbox_inches='tight', pad_inches=0.4)
    worksheet1.insert_image('A' + str(row_num), "", {'image_data': image_data})
    row_num += 23

# Rearrange the sheets
sheet_names = ['Test Execution Progress', 'Test Execution by Date']
workbook.worksheets_objs.sort(key=lambda x: sheet_names.index(x.name))

# Save the workbook
writer.save()

Here is the output:

enter image description here

Note: Names of the Project, Release Name and Cycle Names masked.

Upvotes: 2

Related Questions