-1

I was trying to achieve this

enter image description here

But i ended with this

enter image description here

The main idea was to manage axes to include the third subplot in the figure, but i can't find a way to do it. Can somebody help with that please.

import matplotlib.pyplot as plt

import numpy as np

x = np.arange(-4*np.pi,4*np.pi,0.25)

np.sincx=np.sin(x)/x 

plt.figure(num=3, figsize=(7,5)) 
plt.subplot(3,2,1) 

plt.plot(x,np.sincx)

plt.subplot(3,2,2)

plt.plot(x,np.sincx,"ro")

fig = plt.figure()

ax = fig.add_axes((0.125,0.1,0.775,0.45))

plt.plot(x,np.sincx**2)
1
  • Welcome to SO, @Dearsfs Asfrt. When running your code under Jupyter with Python 3, I am able to see the bottom figure. Commented Nov 28, 2021 at 20:58

1 Answer 1

0

In your code, you are creating two figure, one with the two tops plots, and one with the bottom one. You need to only create one with multiple subplots!

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-4*np.pi,4*np.pi,0.25)
np.sincx = np.sin(x)/x 

ax1 = plt.subplot(221)
ax1.plot(x,np.sincx)

ax2 = plt.subplot(222)
ax2.plot(x,-np.sincx,"ro")

ax3 = plt.subplot(212)
ax3.plot(x, np.sincx**2)

plt.show()

Output

plot

With add_axes()

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-4*np.pi,4*np.pi,0.25)
np.sincx = np.sin(x)/x 

fig = plt.figure() 

ax1 = plt.subplot(221)
ax1.plot(x,np.sincx)

ax2 = plt.subplot(222)
ax2.plot(x,-np.sincx,"ro")

ax3 = fig.add_axes((0.125,0.1,0.775,0.45))
ax3.plot(x, np.sincx**2)

plt.show()

Output add_axes()

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Oh thanks, that helps a lot, i just have one more question, the problem suggest that it will be better if I use this line of code. ax=fig.add_axes((0.125, 0.1, 0.775, 0.45)). I really don't understand how can I implement it on the answer.
If you really want to use this line of code, you can add the line fig = plt.figure() before creating the subplots, and replace the line ax3 = plt.subplot(212) by ax3 = fig.add_axes((0.125,0.1,0.775,0.45)). But you will need to manualy tune the limits of your plot to look good. I'll edit my solution to include it too.
Thanks a lot. I was really stressing out with this, it was simpler than i thought hahaha. Greetings from Chile!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.