【问题标题】:How to add multiple labels on x-axis of groupby plot of pandas dataframe如何在熊猫数据框的groupby图的x轴上添加多个标签
【发布时间】:2018-08-12 09:51:17
【问题描述】:

我在这里共享了一张 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. 为每个组绘制一个图,其中 x 轴为测试执行日期,y 轴为“累积测试”和“累积通过”,显示每个组的测试用例执行进度。
  3. 用相应的项目 ID、发布名称和周期名称组标记 x 轴
  4. 用文本“测试用例计数”标记 y 轴

非常感谢您提供这方面的帮助。

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

在做了一些改变之后,我几乎得到了我想要的东西,如下所示:

现在,我只需要:

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

【问题讨论】:

  • 您能否提供一个仅描述相关子集的minimum working example
  • 我什至不明白这个问题。除了minimal reproducible example,这个问题也缺乏清晰的问题描述。期望的结果是什么?最终的情节应该如何?是什么阻碍了您获得该结果?
  • 我唯一要寻找的是将 x 轴标签添加为项目 ID、发布名称和周期名称
  • 您是说您没有找到制作 xlabel 的方法?喜欢plt.xlabel("mylabel")?严重吗?
  • 刚刚编辑了所有的问题陈述和最少的代码。是的,我尝试过类似于 plt.xlabel("mylabel") 的代码,但它所做的只是只打印一次标签。我需要为每个带有相应组(项目 ID、发布名称和周期名称)的子图设置 x 轴标签

标签: pandas matplotlib plot


【解决方案1】:

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

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

这是输出:

注意:项目名称、发布名称和周期名称被屏蔽。

【讨论】:

    猜你喜欢
    • 2019-06-12
    • 1970-01-01
    • 2020-04-17
    • 2018-08-01
    • 2016-04-10
    • 2016-09-08
    • 2015-11-21
    • 2016-08-17
    • 2020-02-01
    相关资源
    最近更新 更多