Search code examples
pythonmatplotlibsubplot

Figure is plotted on previous subplot figure


I am trying to plot some subplots with matplotlib but for some reason, the figure that I am creating AFTER the subplot is being printed on the last figure of the subplot. I have posted my code beneath with one of the figures as an example As you can see, the last figure of the subplot has another figure plotted over it. This is supposed to be a separate figure

fig4 = plt.subplots(4)
plt.subplot(2, 3, 1)
plt.plot(T34, F34)
plt.plot(x_line342, y_line342, '--', color='red')
plt.title('test 1, 2nd order')

plt.subplot(2, 3, 2)
plt.plot(T34, F34)
plt.plot(x_line343, y_line343, '--', color='red')
plt.title('test 1, 3rd order')

plt.subplot(2, 3, 3)
plt.plot(T34, F34)
plt.plot(x_line344, y_line344, '--', color='red')
plt.title('test 1, 4th order')

plt.subplot(2, 3, 4)
plt.plot(T34, F34)
plt.plot(x_line345, y_line345, '--', color='red')
plt.title('test 1, 5th order')

plt.subplot(2, 3, 5)
plt.plot(T34, F34)
plt.plot(x_line346, y_line346, '--', color='red')
plt.title('test 1, 6th order')
plt.tight_layout()

plt.subplot(2, 3, 6)
plt.plot(T34, F34)
plt.plot(x_line346, y_line346, '--', color='red')
plt.title('test 1, 6th order')
plt.tight_layout()
plt.show

fig, axs = plt.subplots(2, 3)
axs[0, 0].plot(T34, F34)
axs[0, 0].plot(x_line342, y_line342, '--', color='red')
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(T34, F34)
axs[0, 1].plot(x_line343, y_line343, '--', color='red')
axs[0, 1].set_title('Axis [0, 1]')
axs[0, 2].plot(T34, F34)
axs[0, 2].plot(x_line344, y_line344, '--', color='red')
axs[0, 2].set_title('Axis [1, 0]')
axs[1, 0].plot(T34, F34)
axs[1, 0].plot(x_line345, y_line345, '--', color='red')
axs[1, 0].set_title('Axis [1, 1]')
axs[1, 1].plot(T34, F34)
axs[1, 1].plot(x_line346, y_line346, '--', color='red')
axs[1, 1].set_title('Axis [1, 1]')
axs[1, 2].plot(T34, F34)
axs[1, 2].plot(x_line346, y_line346, '--', color='red')
axs[1, 2].set_title('Axis [1, 1]')

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')
for ax in axs.flat:
    ax.label_outer()

#%% plot best curves (4th order)
fig5=plt.plot(5)
plt.plot()
plt.plot(x_line314, y_line314, '--', color='red')
plt.plot(x_line324, y_line324, '--', color='red')
plt.plot(x_line334, y_line334, '--', color='red')
plt.plot(x_line344, y_line344, '--', color='red')
plt.show



print(y_line346)
fig6=plt.plot(6)
plt.plot()
plt.plot(x_line314, y_line314, '--', color='red')
plt.plot(x_line324, y_line324, '--', color='red')
plt.plot(x_line334, y_line334, '--', color='red')
plt.plot(x_line344, y_line344, '--', color='red')
plt.show

Solution

  • You should change all

    plt.show
    

    to

    plt.show()
    

    That may solve your issue.