I have a python array listing all occurences of string labels. Let's call it labels_array. Using seaborn as sns I d like to show a countplot of this array :
sns.countplot(labels_array)
This works, but as they are too many different labels in my array, the outpout doesnt look good.
Is there a way to display only the n most frequent labels.
Although
countplot
should in principle know the counts and hence allow to show only part of them, this is not the case. Therefore, the use of countplot may not make too much sense here.Instead just use a normal pandas plot. E.g. to show the 5 most frequent items in the list,
Complete example:
I came across the same problem (and this question) and found that this question has already been answered.
The
countplot
function has the parameterorder
where you can specify for which values you want to plot the counts. The most often occurred values can be obtained, as previously stated, with thevalue_counts
function.See: limit the number of groups shown in seaborn countplot?