Problem with a Data Science Comulative Project

Getting a empyty png file which is only white
graphs are plotted.
It’s a jupyter Notebook Proect and I’am using it
Thats the code for this part :

plt.close(“all”)
plt.figure(figsize=(24,12))

Left plot Netflix

ax1 = plt.subplot(1, 2, 1)
plt.plot(netflix_stocks[‘Date’],netflix_stocks[‘Price’])
ax1.set_title(“Netflix”)
ax1.set_ylabel(“Stock Price”)
ax1.set_xlabel(“Date”)

Right plot Dow Jones

ax2 = plt.subplot(1, 2, 2)
plt.plot(dowjones_stocks[‘Date’],dowjones_stocks[‘Adj Close’])
ax2.set_title(“Dow Jones”)
ax2.set_ylabel(“Stock Price”)
ax2.set_xlabel(“Date”)

plt.show()
plt.savefig(“filename1.png”)

Hi @frederic_1102 - welcome to the forum.

Can you try swapping the last two lines you’ve posted, like so:

plt.savefig("filename1.png")
plt.show()

Let me know if that fixes it.

:slight_smile:

1 Like

ok it works don’t know show must be behind but it works it’s strange because the save is normally in another step in the capstone project and before is written remember plt.show() to plot

1 Like

Good to know I solved your problem.

The reason why you need to use plt.show() after plt.savefig() is explained in the documentation. Specifically, the “note” under “use show()”:

Note:

Because closing a figure window invokes the destruction of its plotting elements, you should call savefig() before calling show if you wish to save the figure as well as view it.

Your original code had these calls the other way around, so the blank figure was correct behaviour from matplotlib; as it was erasing the plots after plt.show() exited, plt.savefig("filename1.png") had nothing to work with. :slight_smile:

1 Like

I was confused becuase the order to do in the jupyter notebook project is false written. Thx u a lot.

1 Like