How do I plot a histogram using Python so that x-values are frequencies of a spectra?
Question: I looked at the python "matplotlib.pylab" library, and allows me to plot
histograms with the "plt.hist" function. The problem is that it only takes one
data argument, which is an array. In my case, I want to plot a histogram of
the data produced by a fourier transform. A fourier transform shows the
relative quantity of various frequencies. So I could put this array of
quantities into "plt.hist" and get an informative chart, but the x-axis would
not be in the units of frequencies. My guess is that the x-axis would just be
the indices of the array, but even that doesn't seem to be correct when I plot
it. Thanks,


Answer: Naive answer for making a _histogram_ of `X`, the DFT of a time domain signal
`x`

    
    
    import matplotlib.pyplot as plt
    import numpy as np
    
    ...
    w = np.linspace(0,N*dw-dw,N)   
    plt.bar(w, abs(X), align='center', width=dw)
    plt.show()
    

For a nice looking plot, you have to take into account that `X` is associated
with frequencies `0*dw, 1*dw, ..., (N-1)*dw` and that, in a nice looking plot,
you usually want to use a range `-N*dw/2`, `+N*dw/2` for your abscissas.

### Complete answer

    
    
    import matplotlib.pyplot as plt
    import numpy as np
    np.random.seed(57)
    
    N = 64 ; dw = 0.2
    w = np.linspace(0,N*dw-dw,N)
    X = 200 + (np.arange(N)-N/2)**2*np.random.random(N)
    
    plt.bar(w, abs(X), align='center', width=dw)
    plt.xticks([i*8*dw for i in range(N/8)]+[N*dw-dw/2])
    plt.xlim(-dw/2,N*dw-dw/2)
    plt.show()
    

And this is the result so far

![enter image description here](http://i.stack.imgur.com/HfrmZ.png)

as you can see, this type of plot kind of stresses the periodicity of the DFT,
but it is customary to plot the DFT centered around the zero frequency, and
this can be done like this

    
    
    w2=np.concatenate((w-N*dw,w))
    X2=np.concatenate((X,X)
    
    plt.bar(w2, abs(X2), align='center', width=dw)
    plt.xticks([i*8*dw for i in range(-N/16,1+N/16)])
    plt.xlim(-dw*N/2,dw*N/2)
    plt.show()
    

and this is the result ![enter image description
here](http://i.stack.imgur.com/G5Cnz.png)

### Post Scriptum

The procedures I described are good procedures for the OP needs, but I'd like
to say that the `X` data has thoughtlessly been synthesized on the spot, and
has no resemblance with real life DFT. On the contrary, if I see something
like the plots above I'd make a comment on the insufficient sampling rate in
time domain.

