-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)
BTables
  • 4,413
  • 2
  • 11
  • 30
  • Welcome to SO, @Dearsfs Asfrt. When running your code under Jupyter with Python 3, I am able to see the bottom figure. – Sheldon Nov 28 '21 at 20:58

1 Answers1

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

yannvm
  • 418
  • 1
  • 4
  • 12
  • 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. – Dearsfs Asfrt Nov 28 '21 at 21:14
  • 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. – yannvm Nov 28 '21 at 21:22
  • 1
    Thanks a lot. I was really stressing out with this, it was simpler than i thought hahaha. Greetings from Chile! – Dearsfs Asfrt Nov 28 '21 at 21:26