DEV Community

Onaolapo-11
Onaolapo-11

Posted on

If we want to distinguish multiple plots in the same figure in matplotlib, we use the legend function()

Due to work responsibilities, I was unavailable for 11 days thereabout. I'm back now.
I started yesterday (October 22) and completed today (October 23).

Day 64 [October 22, 2025]
I completed day 2 goals, as extracted from the 'Python for Software Development' textbook by Halvorsen (n.d.):

I need to buckle down, as I'm still lagging on day day 3 & 4 goals, "Day 3-4: Control structures (if-else, loops)", as well as day 5 (and 6) goals, "Day 5-6: Functions and modules", and Day 7 target (exercises) (Meta AI, personal communication, August 8, 2025). If I haven't covered this, I can't make progress on day 8 - 63 goals.

Goals:
As extracted from the 'Python for Software Development' textbook by Halvorsen (n.d.):

  • The New Age of Programming ✅
  • What is Python? ✅
  • Introduction to Python ✅
  • Interpreted vs. Compiled ✅
  • Python Packages ✅
  • Python Packages for Science and Numerical Computations ✅
  • Python Editors ✅
  • Python IDLE ✅
  • Visual Studio Code ✅
  • Variables ✅
  • Numbers ✅
  • Strings ✅
  • String Input✅
  • Built-in Functions✅
  • Python Standard Library✅
  • Using Python Libraries, Packages and Modules✅
  • Python Packages✅
  • Plotting in Python ✅
  • Subplots✅
  • Exercises✅
  • If ... Else
  • Arrays
  • For Loops
  • Nested
  • For Loops
  • While Loops
  • Exercises
  • Creating Functions in Python - Introduction
  • Functions with multiple return values
  • Exercises
  • Creating Classes in Python
  • The init () Function
  • Exercises
  • Creating Python Modules
  • Exercises

Notes:
'Python for Software Development' textbook by Halvorsen (n.d.):
On day 62, I worked on the exercise in Halvorsen, n.d., page 51, and came to this solution after corrections:

import matplotlib.pyplot as plt
import numpy as np

xStart = 0
xEnd = 2*np.pi
increment = 0.5

x = np.arange(xStart, xEnd, increment)
y = np.sin(x)
plt.subplot(2,1,1)
plt.plot(x,y,'g')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.title('SINE GRAPH')
plt.grid()
plt.show()

x = np.arange(xStart, xEnd, increment)
y = np.cos(x)
plt.subplot(2,1,2)
plt.plot(x,y,'r')
plt.xlabel('x')
plt.ylabel('cos(x)')
plt.title('COSINE GRAPH')
plt.grid()
plt.show()

But the functions axis() and legend() were not used in my code. I learnt what axis() does basically, but today I will look at legend().

plt.legend() (geeksforgeeks, 2025):

  • To detail the elements in a graph, we set out a space, known as Legend.
  • To place this Legend in matplotlib we use the legend() function.
  • If we want to distinguish multiple plots in the same figure, we use the legend function().
  • To specify the location of the legend, the loc attribute is used, e.g. loc = "upper left", loc = "lower right", etc.
  • For multiple plots, the name of the labels are put in a list, e.g. plt.legend(["Bus Fee", "Salary"], loc = "lower right") [Code extracted from geeksforgeeks, 2025; and adjusted a bit]: import numpy as np import matplotlib.pyplot as plt

Y-axis values

y1 = [2,3,4.5]

Y-axis values

y2 = [1,1.5,5]

Function to plot

plt.plot(y1)
plt.plot(y2)

Function add a legend

plt.legend(["Bus Fee", "Salary"], loc = "lower right")

Summary:
If we want to distinguish multiple plots in the same figure, we use the legend function().

References:

  1. geeksforgeeks. (2025, July 12). Matplotlib.pyplot.legend() in python. https://www.geeksforgeeks.org/python/matplotlib-pyplot-legend-in-python/

  2. Halvorsen, H. (n.d.). Python. https://halvorsen.blog/documents/programming/python/python.php#python4

Top comments (0)