Two questions dominate categorical charts, and they look alike. "How many rows are in each category?" is a count, and countplot draws it. "What is the average of a number in each category?" is a summary, and barplot computes it and draws it. Mixing them up is easy because both produce bars, and because barplot quietly draws error bars unless told not to, so a bar chart of means is also a chart of uncertainty whether you asked or not.
The other decision is order. Seaborn lists categories in the order they first appear in the data, which is usually accidental. Putting them in a deliberate order (alphabetical, or ranked by value) is what makes a chart readable, and this question builds both the ranking and the charts that use it.
Implement ranked_order(df, cat, value), mean_bars(df, cat, value, order), count_bars(df, cat, hue, order, hue_order) and horizontal_means(df, cat, value, order). The signatures and docstrings are already in the editor.
ranked_order(df, cat, value) returns the categories of cat as a list, ordered by the mean of value from largest to smallest, ties by category name ascending. Missing values of value are ignored in the mean.mean_bars(df, cat, value, order) draws one bar per category in order, whose height is the mean of value in that category, without error bars. Returns the Axes.count_bars(df, cat, hue, order, hue_order) draws, for each category in order, one bar per value in hue_order whose height is the number of rows with that category and hue. Returns the Axes.horizontal_means(df, cat, value, order) is mean_bars drawn horizontally: categories on the y axis in order (top to bottom), the mean as the bar length on the x axis, no error bars. Returns the Axes.plt.show().df.groupby(cat)[value].mean() gives the means. Sort by (-mean, name) to get the ranked order.
sns.barplot(..., estimator="mean", errorbar=None, order=...) draws means without error bars. sns.countplot(..., hue=...) draws counts.
A horizontal bar plot swaps the roles: put the numeric column on x and the category on y.
No results yet
Run your code to check it against the examples, or submit to run every test.