im trying to plot the sum of 2 graphs. I did this easily by combining the y values when it was an array of numbers or a straight line but now im using the same graph only slightly shifted and the y values are determined by a function so im not sure what to so. Here is what i have so far and also some of my attempts are #'d.
import matplotlib.pyplot as plt
import numpy as np
h=6.626e-34
c= 3.0e+8
k= 1.38e-23
#wav is the wavelength
def planck(wav, T):
a= 2.0*h*c**2
b= (h*c)/(wav*k*T)
intensity = a/( (wav**5) * (np.exp(b)- 1.0) )
return intensity
wavelengths= np.arange(1e-9, 3e-6, 1e-9)
#wave=np.arange((1+500)*10**-9,3e-6,1e-9)
intensity6000= planck( wavelengths, 6000.)
#intensity6001=planck(wavelengths+(500*10**-9),6000)
#sum_of_values=intensity6000+
plt.plot(wavelengths*1e9, intensity6000, 'm-', label= '6000 K')
plt.plot(wavelengths*1e9+500, intensity6000, 'b', label='6000k shifted')
#plt.plot(wavelengths*1e9+wavelengths*1e9+500, intensity6000) this is wrong
it shifts it again doesnt show the total
#plt.plot(wavelengths*1e9+500,intensity6001) #plots a straight line at 0
plt.xlabel('Wavelength\n(nm)')
plt.ylabel('Intensity\n(W Sr^-1 m^-3)')
plt.title('Black Body Radiation')
plt.legend()
plt.show()
One way you could approach this is by padding with 0 at the beginning (using np.concatenate((np.zeros(shift),intensity))
) of the shifted data and removing the same amount of data at the end (the slicing [:-shift]
) prior to summing both datasets.
A cleaned up version of your code is as follows
import matplotlib.pyplot as plt
import numpy as np
h=6.626e-34
c= 3.0e+8
k= 1.38e-23
shift = 500 # defining the shift
#wav is the wavelength
def planck(wav, T):
a= 2.0*h*c**2
b= (h*c)/(wav*k*T)
intensity = a/( (wav**5) * (np.exp(b)- 1.0) )
return intensity
wavelengths= np.arange(1e-9, 3e-6, 1e-9)
intensity = planck( wavelengths, 6000.)
# creating a shifted intensity by padding 0 in the begining
# and removing extra values at the end
shifted_intensity = np.concatenate((np.zeros(shift),intensity))[:-shift]
sum_intensity = intensity + shifted_intensity
plt.plot(wavelengths*1e9, intensity, 'm-', label= '6000 K')
plt.plot(wavelengths*1e9, shifted_intensity, 'b-', label='6000 K shifted')
plt.plot(wavelengths*1e9, sum_intensity, 'r-', label= 'Summed intensity')
plt.xlabel('Wavelength\n(nm)')
plt.ylabel('Intensity\n(W Sr^-1 m^-3)')
plt.title('Black Body Radiation')
plt.legend()
plt.show()
There might be a simpler way, but this is a rather straightforward way.