Hey.. I have run into a bit of a problem with my python code.. I have a set of values each for frequency and power-spectrum. I need to plot frequency v/s power-spectrum on Log scale. I was trying to store the logarithmic values of frequency and power-spectrum in 2 other variables and then plot them.. Any idea how that can be done?
2 Answers
Assuming you've got a list of values you might use a simple list comprehension:
frequencies = [1, 2, 3, 4, 5]
import math
logOfFrequencies = [ math.log(x) for x in frequencies ]
Or
logOfFrequencies = map(math.log, frequencies)
If you have a numpy array of frequencies because you're using Matplotlib / Pylab to create your plots you can instead do:
import numpy
frequencies = numpy.arange(1, 5)
logOfFrequencies = numpy.log(frequencies)
Comments
If you are only interested in plotting the data on a log scale, consider the matplotlib methods, loglog and semilogx and semilogy:
http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.loglog
http://matplotlib.sourceforge.net/plot_directive/mpl_examples/pylab_examples/log_demo.py
This would allow you to avoid calculating the log of the various arrays and allow you to customize exactly how the various quantities are displayed.