I thought I’d create this post to share my knowledge and complete my answer to the question from Kamal in today’s session. So, to show a breakdown of .annotate method from matplotlib.pyplot library to clarify further my answer that I posted in the chat today for Machine Learning course (13th Jan 2018 session):
import matplotlib.pyplot as plt
plt.annotate(s = “Five”, xy=(5,5), xytext=(2,5), color = ‘red’, arrowprops=dict(facecolor=‘red’))
Following is a breakdown of the above arguments only as the annotate method has many arguments which you can go and see by saying help(plt.annotate):
s is the text label you want to display as a string (you can just have the string as the first argument without s= in front)
xy= tuple with xycoordinates of the point you are labelling
xytext = tuple with xycoordinates where the text label will appear
color = the font color of the text label
arrowprops is used to draw an arrow pointing from the xytext (position of label) to xy (the point you are referencing).
arrowprops has various properties which are passed to it as a dictionary. I used the facecolor property to make the red arrow as our confusion matrix image was greyscale. Red will make it stand out. Same logic behind making the text color red.
For more resources on learning data visualisation:
- I highly recommend the Python data visualisation courses on DataCamp which go through matplotlib, seaborn and bokeh. This is where I learnt about this annotate method
- Also below: is a link to some cheat sheets I posted earlier which has matplotlib cheat sheet.
Machine Learning Python library cheat sheets - see matplotlib cheat sheet in this post