For data sets that vary exponentially in the independent variable, it is often useful to use one or more logarithmic axes. Radioactive decay of unstable nuclei, for example, exhibits an exponential decrease in the number of particles emitted from the nuclei as a function of time. Moreover, if the data vary exponentially in time, then the data will
fall along a straight line, as they do for the case of radioactive
decay. matplotlib provides two functions for making semi-logarithmic
plots, semilogx and semilogy, for creating plots with logarithmic x and y
axes, with linear y and x axes, respectively. We illustrate their use in the program below:
import numpy as np
import matplotlib.pyplot as plt
# read data from file
time, counts, unc = np.loadtxt('semilogDemo.txt',
unpack=True)
# create theoretical fitting curve
t_half = 14. # P-32 half life = 14 days
tau = t_half/np.log(2) # exponential tau
N0 = 8200. # Initial count rate (per sec)
t = np.linspace(0, 180, 128)
N = N0 * np.exp(-t/tau)
# create plot
plt.figure(1, figsize=(9.5, 4))
plt.subplot(1, 2, 1)
plt.plot(t, N, color='C0', label="theory")
plt.plot(time, counts, 'oC1', label="data")
plt.xlabel('time (days)')
plt.ylabel('counts per second')
plt.legend(loc='upper right')
plt.subplot(1, 2, 2)
plt.semilogy(t, N, color='C0', label="theory")
plt.semilogy(time, counts, 'oC1', label="data")
plt.xlabel('time (days)')
plt.ylabel('counts per second')
plt.legend(loc='upper right')
plt.tight_layout()
# save plot to file
plt.savefig('figures/semilogDemo.pdf')
# display plot on screen
plt.show()
When we run this program we get the following plot:
In Fig. shown above, we show the decay of the radioactive isotope Phosphorus-32 over a period of 6 months, where the radioactivity is measured once each week. Starting at a decay rate of nearly 104 electrons (counts) per second, the decay rate diminishes to only about 1 count per second after about 6 months or 180 days. If we plot counts per second as a function of time on a normal plot, as we have done in the left panel of Fig. shown above, then the count rate is indistinguishable from zero after about 100 days. On the other hand, if we use a logarithmic axis for the count rate, as we have done in the right panel of Fig. shown above, then we can follow the count rate well past 100 days and can readily distinguish it from zero.
The semilogx and semilogy functions work the same way as the plot function. We just use one or the other depending on which axis we want to be logarithmic. You may have noticed the tight_layout() function, called without arguments on line 32 of the program. This is a convenience function that adjusts the space around the subplots to make room for the axes labels. If it is not called in this example, the y-axis label of the right plot runs into the left plot. The tight_layout() function can also be useful in graphics windows with only one plot sometimes.
If you want more control over how much space is allocated around subplots, use the function
plt.subplots_adjust(left=None, bottom=None, right=None,top=None, wspace=None, hspace=None)
The keyword arguments wspace and hspace control the width and height of the space between plots, while the other arguments control the space to the left, bottom, right, and top. You can see and adjust
the parameters of the subplots_adjust() routine by clicking on the configure subplots icon in the figure window. Once you have adjusted the parameter to obtain the desired effect, you can then use
them in your script.
import numpy as np
import matplotlib.pyplot as plt
# read data from file
time, counts, unc = np.loadtxt('semilogDemo.txt',
unpack=True)
# create theoretical fitting curve
t_half = 14. # P-32 half life = 14 days
tau = t_half/np.log(2) # exponential tau
N0 = 8200. # Initial count rate (per sec)
t = np.linspace(0, 180, 128)
N = N0 * np.exp(-t/tau)
# create plot
plt.figure(1, figsize=(9.5, 4))
plt.subplot(1, 2, 1)
plt.plot(t, N, color='C0', label="theory")
plt.plot(time, counts, 'oC1', label="data")
plt.xlabel('time (days)')
plt.ylabel('counts per second')
plt.legend(loc='upper right')
plt.subplot(1, 2, 2)
plt.semilogy(t, N, color='C0', label="theory")
plt.semilogy(time, counts, 'oC1', label="data")
plt.xlabel('time (days)')
plt.ylabel('counts per second')
plt.legend(loc='upper right')
plt.tight_layout()
# save plot to file
plt.savefig('figures/semilogDemo.pdf')
# display plot on screen
plt.show()
When we run this program we get the following plot:
In Fig. shown above, we show the decay of the radioactive isotope Phosphorus-32 over a period of 6 months, where the radioactivity is measured once each week. Starting at a decay rate of nearly 104 electrons (counts) per second, the decay rate diminishes to only about 1 count per second after about 6 months or 180 days. If we plot counts per second as a function of time on a normal plot, as we have done in the left panel of Fig. shown above, then the count rate is indistinguishable from zero after about 100 days. On the other hand, if we use a logarithmic axis for the count rate, as we have done in the right panel of Fig. shown above, then we can follow the count rate well past 100 days and can readily distinguish it from zero.
The semilogx and semilogy functions work the same way as the plot function. We just use one or the other depending on which axis we want to be logarithmic. You may have noticed the tight_layout() function, called without arguments on line 32 of the program. This is a convenience function that adjusts the space around the subplots to make room for the axes labels. If it is not called in this example, the y-axis label of the right plot runs into the left plot. The tight_layout() function can also be useful in graphics windows with only one plot sometimes.
If you want more control over how much space is allocated around subplots, use the function
plt.subplots_adjust(left=None, bottom=None, right=None,top=None, wspace=None, hspace=None)
The keyword arguments wspace and hspace control the width and height of the space between plots, while the other arguments control the space to the left, bottom, right, and top. You can see and adjust
the parameters of the subplots_adjust() routine by clicking on the configure subplots icon in the figure window. Once you have adjusted the parameter to obtain the desired effect, you can then use
them in your script.
0 comments:
Post a Comment