【发布时间】:2018-03-03 22:01:29
【问题描述】:
注意:full reproduction notebook for this question 可以在 GitHub 上找到。
我有一个数据集,其中包含我想按类分组的 HTTP 响应代码分布。样本数据可以这样生成:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
mock_http_response_data = pd.DataFrame({
'response_code':np.repeat([200, 201, 202, 204, 302, 304, 400, 404, 500, 502], 250 ),
})
我在数据中添加了一个基于“响应状态”的列,称为“响应类”。响应类包含与特定响应的类对应的标签:
- 2xx:成功
- 3xx:警告
- 4xx:客户端错误
- 4xx:服务器错误
判断响应类的函数是:
def determine_response_class(row):
response_code = row['response_code']
if response_code >= 200 and response_code < 300:
return 'success'
elif response_code >= 300 and response_code < 400:
return 'warning'
elif response_code >= 400 and response_code < 500:
return 'client_error'
elif response_code >= 500 and response_code < 600:
return 'server_error'
else:
return 'unknown'
并且该列是这样添加的:
# Add 'Response class' column to API Logs, where response class is determined by HTTP status code
mock_http_response_data['response_class'] = mock_http_response_data.apply(determine_response_class, axis='columns')
“响应状态”(HTTP 状态代码)数据使用基本计数图正确绘制:
sns.countplot(
x='_source.response_status',
data=results_df,
color='teal',
saturation=0.7)
当我尝试创建计数图的 FacetGrid 时,图表似乎可以工作,但标签不正确:
grid = sns.FacetGrid(mock_http_response_data, col='response_class')
grid.map(sns.countplot, 'response_code')
我希望计数图的 FacetGrid 将具有以下 x 轴标签:
- 200
- 201
- 202
- 302
- 304
- 400
- 404
- 500
- 502
如何创建计数图的 FacetGrid,以便标签正确且分面数据从高到低排序(例如“成功”类列)?
【问题讨论】:
-
创建一个minimal reproducible example 的问题怎么样?如果您声称的标签不正确,其他人怎么会知道?
-
问题中的图片描述了数据。第一个图表显示带有正确(x 轴)标签的数据的整体分布,第二个图表只是将数据分成四个部分(2xx、3xx、4xx、5xx)。如果你垂直比较图表,你会发现它们有很强的对应关系,但第二张图片的标签不正确。
-
我在原始问题中添加了尽可能多的细节,但没有公布实际数据。
-
好吧,也许你没有明白我的意思。您基本上是在要求某人创建一些数据框来重现该问题,这可能是可能的,但会浪费时间。相反,如果您自己创建一些数据并提供minimal reproducible example,人们会更倾向于帮助您。最后当然是你的选择。
-
我为这个问题添加了一个完整的复制笔记本,包括数据:github.com/brylie/jupyter_http_status_code_visualization/blob/…
标签: python pandas matplotlib jupyter-notebook seaborn