DEV Community

Onaolapo-11
Onaolapo-11

Posted on

When applying the subplot() function in Matplotlib, its position is important

I did the reading on Day 63.
Day 62 [October 8, 2025]

I need to buckle down, as I'm still lagging on day 2's remaining goals, 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 - 61 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.):
Today, I worked on the following exercise (Halvorsen, n.d.):
"Create sin(x) and cos(x) in 2 different plots.
You should use all the Plotting functions listed below in your code:
• plot()
• title()
• xlabel()
• ylabel()
• axis()
• grid()
• legend()
• show()"

After trial and error, here is my code:

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.plot(x,y,'g')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.title('SINE GRAPH')
plt.subplot(2,1,1)
plt.grid()
plt.show()

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

However, it is off, from what they did in their own previous similar example as shown:
import matplotlib.pyplot as plt
import numpy as np

xstart = 0
xstop = 2*np.pi
increment = 0.1

x = np.arange(xstart, xstop, increment)

y = np.sin(x)

z = np.cos(x)

plt.subplot(2,1,1)
plt.plot(x,y,'g')
plt.title('sin')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.grid()
plt.show()

plt.subplot(2,1,2)
plt.plot(x,z,'r')
plt.title('cos')
plt.xlabel('x')
plt.ylabel('cos(x)')
plt.grid()
plt.show()

---Mine was causing the plot to look funny.

I noticed that difference basically was the positioning of plt.subplot(2,1,1) and plt.subplot(2,1,2) respectively.

Rearranging mine, also solved the problem:
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()

The functions axis() and legend() were not also used in my code. Quite frankly, I have no idea what they do. I'll begin my study on these tomorrow.

Summary:
When applying the subplot() function in Matplotlib, its position is important.

References:

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

Top comments (0)