如何在pandas数据帧的groupby图的x轴上添加多个标签

时间:2018-03-04 18:08:44

标签: pandas matplotlib plot

我在这里分享了Excel表格: https://docs.google.com/spreadsheets/d/1WolE-TpyEXtv1rlr3xusESke46UMlRurAEH1D1Lsyss/edit?usp=sharing

我正在尝试创建工作进度图,这是我的代码:

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')

目标是:

  1. 按项目ID,版本名称和周期名称
  2. 对数据框进行分组
  3. 对于每个组,在x轴上绘制带有测试执行日期的图和"累积测试"和"累计通过"在y轴上显示每个组的测试用例执行进度。
  4. 使用相应的项目ID,版本名称和周期名称组标记x轴
  5. 使用文本标记y轴,"测试用例计数"
  6. 对此方面的帮助表示高度赞赏。

    修改 我从这里找到了一个例子: enter link description here

    经过一些改动之后,我几乎得到了我想要的东西如下: enter image description here

    现在,我只需要:

    1. 从每个图表顶部的元组中删除括号(例如:(' BIC',' R5'' C5')在顶部第一个情节)我认为我可以自己做。
    2. 删除每个图表左侧的重复yticks。我对此一无所知。

1 个答案:

答案 0 :(得分:0)

最后,我解决了所有问题,并设法获得所需的输出。这是代码:

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

这是输出:

enter image description here

注意:项目名称,版本名称和周期名称已被屏蔽。