JA-pythonista
JA-pythonista

Reputation: 1323

How to manually add labels to matplotlib with Python

I am trying to manually add labels to my seaborn plot, but this is not outputting the labels

Here is what my dataframe looks like:

import pandas as pd
df = pd.DataFrame({'Product_ID': ['20','23', '20','24','24', '24', '24', '24','24', '20'],
 'ProductNames': ['Gem','Wil', 'Gem','Wefdem','Wefdem','Wefdem', 'Wefdem', 'Wefdem', 'Wefdem','Gem']})

Here is my item to plot and manually add labels

# It is a large dataframe, this is just a subset thats why I selected the first largest 10 with nlargest

order=df["Product_ID"].value_counts().nlargest(10).index

# again the actual dictionary is larger than this, this is just a shorter one of it
product_keys = {"20": "Gem", "23": "Wil", "24": "Wefdem"}

plt.figure(figsize=(20,20))
g = sns.countplot(df["Product_ID"], order=order)
g.axes.set_title("Most popular IDs", fontsize=40)
g.axes.set_xlabel("Product IDs", fontsize=30)
g.axes.set_ylabel("Frequency count", fontsize=30)
g.tick_params(labelsize=24)

plt.legend([g], [product_keys[key] for key in order]) # <--- This does not work or shows only one label

Please how can I solve this issue?

Upvotes: 0

Views: 613

Answers (1)

cvanelteren
cvanelteren

Reputation: 1703

The seaborn docs state that

order, hue_orderlists of strings, optional Order to plot the categorical levels in, otherwise the levels are inferred from the data objects.

The thing to wrap your head around is that seaborn interfaces with objects from matplotlib. As such I recommend first learning matplotlib before delving into seaborn. The countplot function looks for an active figure which you created by plt.figure. The hue argument looks for levels in the data that are in the second column of your dataframe.

I took the liberty to make your code a bit more pythonic:

import pandas as pd
df = pd.DataFrame({'Product_ID': ['20','23', '20','24','24', '24', '24', '24','24', '20'],
 'ProductNames': ['Gem','Wil', 'Gem','Wefdem','Wefdem','Wefdem', 'Wefdem', 'Wefdem', 'Wefdem','Gem']})

import matplotlib.pyplot as plt,\
seaborn as sns
order=df["Product_ID"].value_counts().nlargest(10).index

# again the actual dictionary is larger than this, this is just a shorter one of it
product_keys = {"20": "Gem", "23": "Wil", "24": "Wefdem"}
fig, ax = plt.subplots()
sns.countplot(x = "Product_ID", data = df,\
              order = order, ax = ax,\
              hue = "ProductNames" )
ax.set_title("Most popular IDs", fontsize=40)
ax.set_xlabel("Product IDs", fontsize=30)
ax.set_ylabel("Frequency count", fontsize=30)
ax.axes.tick_params(labelsize=24)
fig.show()

enter image description here

Upvotes: 2

Related Questions