1

I have this code to produce three plots. The code runs fine and produces all three plots as the way I wanted. However, when I tried to save the plot, what I get is only the image of the last plot which is a plot for G0 and not the image of the plots showing one after another. I tried using plt.show() instead of using plt.savefig() and plt.figure, but that returns me a white(blank) image. Can someone please help?

import numpy as np import matplotlib.pylab as plt
def planks_law(wave, T):
    h = 6.626e-34
    c = 3e8
    k = 1.38e-23
    bb = (2*h*c**2)/(wave**5)* (1/(np.exp((h*c)/(wave*k*T)-1)))
    return bb

b0 = np.genfromtxt("filename.txt") plt.plot(b0[:, 0]*1e-10,b0[:, 1],label='B0 Data')
bb_b0 = planks_law(b0[:,0]*1e-10, 30000)
scale_bb = np.trapz(b0[:,1],b0[:,0]*1e-10) / np.trapz(bb_b0,b0[:,0]*1e-10)
bb_b0_scaled = bb_b0 * scale_bb
plt.plot(b0[:,0]*1e-10, bb_b0_scaled,label='B0 Theoretical')
plt.xlabel('Wavelength [m]') plt.ylabel('I(λ,T)') 
plt.title('Blackbody Curve Fit to Known Spectra')
plt.legend(loc='upper right')
plt.savefig('filename.jpg') 
plt.figure()

f0 = np.genfromtxt("filename.txt") plt.plot(f0[:, 0]*1e-10,f0[:, 1],label='F0 Data')
bb_f0 = planks_law(f0[:,0]*1e-10, 7200)
scale_bb = np.trapz(f0[:,1],f0[:,0]*1e-10) / np.trapz(bb_f0,f0[:,0]*1e-10)
bb_f0_scaled = bb_f0 * scale_bb
plt.plot(f0[:,0]*1e-10, bb_f0_scaled,label='F0 Theoretical')
plt.xlabel('Wavelength [m]') plt.ylabel('I(λ,T)')
plt.legend(loc='lower right')

plt.savefig('filename.jpg') 
plt.figure()

g0 = np.genfromtxt("filename.txt") plt.plot(g0[:, 0]*1e-10,g0[:, 1],label='G0 Data')
bb_g0 = planks_law(g0[:,0]*1e-10, 6000)
scale_bb = np.trapz(g0[:,1],g0[:,0]*1e-10) / np.trapz(bb_g0,g0[:,0]*1e-10)
bb_g0_scaled = bb_g0 * scale_bb
plt.plot(g0[:,0]*1e-10, bb_g0_scaled,label='G0 Theoretical')
plt.xlabel('Wavelength [m]') plt.ylabel('I(λ,T)')
plt.legend(loc='lower right')

plt.figure()
plt.savefig('filename.jpg')

What I essentially want to get as the attached image. enter image description here

William Miller
  • 9,839
  • 3
  • 25
  • 46
CapKoko
  • 49
  • 1
  • 5
  • you can't save with the same name - it will remove previous image. You can save it with different names and later use other tool (like ImageMagick) or python module to join all image in one image. OR you would have to plot all with one figure and many subplots and then it can be saved as one file. – furas Dec 08 '19 at 06:13
  • Does this answer your question? [How do I get multiple subplots in matplotlib?](https://stackoverflow.com/questions/31726643/how-do-i-get-multiple-subplots-in-matplotlib) – Yuri Feldman Dec 08 '19 at 06:16
  • Note that [the official guide](https://matplotlib.org/faq/usage_faq.html#matplotlib-pyplot-and-pylab-how-are-they-related) recommends against the use of `matplotlib.pylab`. `matplotlib.pyplot` should be used directly instead. – William Miller Dec 08 '19 at 06:36

2 Answers2

3

You're plotting to separate figures, that's why you can only save them separately. Try using subplots to plot to a single figure. There are two options for this:

  1. Using "state machine" interface, plt.subplot(#rows, #cols, plot id)
import matplotlib.pyplot as plt 

plt.figure(figsize=(adjust size), **other_fig_args) 

plt.subplot(3, 1, 1)
plt.plot(...) # top plot

plt.subplot(3, 1, 2)
plt.plot(...) # middle plot


plt.subplot(3, 1, 3)
plt.plot(...) # bottom plot

plt.savefig(...)  # will now save all plots

  1. Using "object oriented" interface, plt.subplots(#rows, #cols, figsize=(adjust size), **other_fig_args)

fig, axes = plt.subplots(3, 1, figsize=(adjust size), **other_fig_args)
axes[0].plot(...) # top plot
axes[1].plot(...) # middle plot
axes[2].plot(...) # bottom plot

plt.savefig(...)  # will now save all plots

Yuri Feldman
  • 2,354
  • 1
  • 20
  • 23
2

You need to use subplots

You can stack two plots vertically like this

fig, axs = plt.subplots(2)
fig.suptitle('Vertically stacked subplots')
axs[0].plot(x, y)
axs[1].plot(x, -y)

Then when you save the plot you will have all three plots in the same image.

Read more about subplots here

abhilb
  • 5,639
  • 2
  • 20
  • 26