Adding labels and legends

Introduction to Data Science in Python

Hillary Green-Lerman

Lead Data Scientist, Looker

What did we just plot?

from matplotlib import pyplot as plt

plt.plot(ransom.letter,
         ransom.frequency)

plt.show()

Introduction to Data Science in Python

Axes and title labels

plt.xlabel("Letter")
plt.ylabel("Frequency")
plt.title("Ransom Note Letters")

Labels anywhere before plt.show()

Introduction to Data Science in Python

Legends

plt.plot(aditya.days,
         aditya.cases,
         label="Aditya")
plt.plot(deshaun.days,
         deshaun.cases,
         label="Deshaun")
plt.plot(mengfei.days,
         mengfei.cases,
         label="Mengfei")
plt.legend()

Introduction to Data Science in Python

Arbitrary text

plt.text(xcoord,
    ycoord,
    "Text Message")
plt.text(5,
    9,
    "Unusually low H frequency!")

Introduction to Data Science in Python

Modifying text

  • Change font size
    plt.title("Plot title", fontsize=20)
    
  • Change font color
    plt.legend(color="green")
    

https://en.wikipedia.org/wiki/Web_colors

Introduction to Data Science in Python

Let's practice!

Introduction to Data Science in Python

Preparing Video For Download...