Note: A full reproduction notebook for this question can be found on GitHub.
I have a data set with a distribution of HTTP response codes that I would like to group by class. Sample data can be generated like so:
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 ),
})
I have added a column to the data, based on 'response status', called 'response class'. The response class contains the label corresponding to the class of the particular response:
- 2xx: success
- 3xx: warning
- 4xx: client error
- 4xx: server error
The function to determine the response class is:
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'
And the column is added like so:
# 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')
The 'response status' (HTTP status code) data plots correctly with a basic countplot:
sns.countplot(
x='_source.response_status',
data=results_df,
color='teal',
saturation=0.7)
When I try to create a FacetGrid of countplots, the charts seem to work, but the labels are incorrect:
grid = sns.FacetGrid(mock_http_response_data, col='response_class')
grid.map(sns.countplot, 'response_code')
I would expect that the FacetGrid of countplots would have the following x-axis labels:
- 200
- 201
- 202
- 302
- 304
- 400
- 404
- 500
- 502
How can I create a FacetGrid of countplots so that the labels are correct and the faceted data are sorted from high to low (e.g. the 'success' class column)?