How to add text point labels to Matplotlib and Seaborn?

In today data visualization we’ll show hot you can quickly add label to data points to a chart that would like to display.

We’ll show how to work with labels in both Matplotlib (using a simple scatter chart) and Seaborn (using a lineplot).

We’ll start by importing the Data Analysis and Visualization libraries: Pandas, Matplotlib and Seaborn.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

Create the example data

We’ll now define a very simple dataset that we’ll use in the example:

languages = ['C#', 'Go', 'Ruby', 'SQL']
avg_salary = [78, 83, 84, 75]
candidates = [80,87,79, 75]

Adding scatter label texts with Matplotlib

In this example we’ll first render our plot and then use the plt.text() method to add the point labels at the specific required coordinates on the graph.

# Draw the graph
plt.scatter(avg_salary, candidates);

# Loop through the data points 
for i, language in enumerate (languages):
    plt.text(avg_salary[i]+0.2, candidates[i]+0.2, language)

plt.xlim (70, 90)
plt.ylim (70, 90);

Here is our simple Scatter including the plot label points text:

Seaborn text for data point labels

In this example, we’ll first generate a DataFrame and use the very powerful Seaborn library to plot the chart and annotate the plot points.

# Define Dataframe
hr = pd.DataFrame(dict(language =languages,avg_salary=avg_salary, candidates=candidates  ))

# Define variables for chart
languages = hr['language']
avg_salary = hr['avg_salary']
candidates = hr['candidates']

Note the usage of kwargs (Keyword Arguments) to pass the Line2D styling property values. In our case we customize the mark size and type, line width, line color and style.

# Keyword arguments for styling the plot
kwargs = dict (linestyle='--', color='b', marker ='o', linewidth=1.2, markersize=13)

# Draw the plot
line = sns.lineplot(x = 'avg_salary', y = 'candidates', data=hr,**kwargs)

# Annotate label points 
for i, language in enumerate (languages):
    plt.annotate(language, (avg_salary[i]+0.7, candidates[i]+0.5) )
line.set_xlim (70, 90)
line.set_ylim (70, 90)

Additional Learning