0

I have been trying to save my figures using the following code, but the figure being saved in the directory is just blank. What mistake am I doing?

The code I am using is:

import matplotlib as mpl
mpl.matplotlib_fname()
musti = "/Users/Mustafa/Project RS 2/XRF.csv"
df = pd.read_csv(musti)
df

fig = plt.figure(figsize = (3,5))
plt.plot(Cl1, depth, color= "blue", linewidth=1, label='Cl1')
plt.plot(Cl2, depth, color= "green", linewidth=1, label='mean')
plt.plot(Cl3, depth, color= "red", linewidth=1, label='mean')
plt.plot(Cl4, depth, color= "brown", linewidth=1, label='mean')
plt.plot(Cl5, depth, color= "black", linewidth=1, label='mean')
plt.xlabel('Counts')
plt.ylabel('Depth')
plt.ylim(1000, 0)
plt.xlim(750, 2000)
plt.grid(True)
plt.legend(loc=4)
plt.show()
plt.savefig("C:/Users/Mustafa/Python Project/musti.png", bbox_inches="tight", dpi=300, pad_inches=2, transparent=True)
Tonechas
  • 13,398
  • 16
  • 46
  • 80

1 Answers1

0

You should switch the order of the last 2 lines. If you show the plot first, it is 'consumed' and there is nothing to save.

plt.savefig("C:/Users/Mustafa/Python Project/musti.png", bbox_inches="tight", dpi=300, pad_inches=2, transparent=True)

plt.show()
pakpe
  • 5,391
  • 2
  • 8
  • 23