How do you add labels to a scatter plot in python?
Step by step guide to how to add text labels to scatterplot in python when using seaborn or matplotlib libraries
Python is great for data visualization! Matplotlib is very fast and robust but lacks the aesthetic appeal. Seaborn library built over matplotlib has greatly improved the aesthetics and provides very sophisticated plots. However when it comes to scatter plots, these python libraries do not have any straight forward option to display labels of data points. This feature is available in other data visualization tools like Tableau and Power BI, with just a few clicks or hovering the pointer over the datapoints.
In this article, I will explain how to add text labels to your scatter plots made in seaborn or any other library which is built on matplotlib framework.
The dataset is English Premier League
table. We are interested in three columns:
Scatter Plot : Goals Scored vs Goals Conceded
A simple scatter plot can plotted with Goals Scored in x-axis and Goals Conceded in the y-axis as follows.
Basic scatter plot
Label Specific Items
Most often scatter plots may contain large amount of data points, we might be interested how some specific items fare against the rest. Labelling all the data points may render your plot too clunky and difficult to comprehend.
He x and y are Goals scored and Goals conceded by TOT respectively. The string to be displayed is “TOT”.
Additional arguments like color, size, alpha(transperency) etc. can be used to change to text format. It can also be grouped within fontdict to make your code easy to read and understand.
Scatter Plot with specific label (Image by author)
Adding Background Box
bbox parameter can be used to highlight the text.
Note that an indentation of 0.3 is added to x and y coordinates so that the text and the background box does not overlap with the datapoint.
Scatter Plot with Text Box (Image by author)
Labelling All Points
situations demand labelling all the datapoints in the scatter plot especially when there are few data points.
sns.scatterplot(data=df,x=’G’,y=’GA’)for i in range(df.shape):
Scatter Plot with all labels (Image by author)
completed constructing a labelled scatter plot. However, we can observe that a few text boxes are jutting out of the figure area.
Padded Scatter Plot with labels (Image by author)
If you know any better methods of wrapping the elements in plot within the canvas area please let me know in comments.
You can check out the notebook for this article in GitHub.
Become a Member
I hope you like the article, I would highly recommend signing up for Medium Membership to read
more articles by me or stories by thousands of other authors on variety of topics.
Here are some other stories you may be interested in.Photo by Michael Dziedzic on Unsplash
How do you add a label to a scatter plot?
Do add the data labels to the scatter chart, select the chart, click on the plus icon on the right, and then check the data labels option. This will add the data labels that will show the Y-axis value for each data point in the scatter graph.
How do you add labels to graphs in Python?
With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x- and y-axis..
Add labels to the x- and y-axis: import numpy as np. ... .
Add a plot title and labels for the x- and y-axis: import numpy as np. ... .
Set font properties for the title and labels: import numpy as np. ... .
Position the title to the left:.
How do I add data labels in Matplotlib?
Add Value Labels on Matplotlib Bar Chart Using pyplot..
The parameter text is the label that will be added to the graph..
The parameter xy accepts a tuple (x,y) where x and y are the coordinates where the label will be added to the graph..
The function accepts many different arguments..
How do you add text to a scatter plot?
Create Text Scatter Plot Plot a string array of numbers at random points on a text scatter plot. x = rand(50,1); y = rand(50,1); str = string(1:50); figure textscatter(x,y,str); Alternatively, you can pass the coordinates x and y as a matrix xy , where x and y are the columns of xy .