https://steamforge.net/wiki/index.php?title=How_To_set_a_Python_matplotlib_axis_to_display_from_-Pi_to_Pi&feed=atom&action=history How To set a Python matplotlib axis to display from -Pi to Pi - Revision history 2022-01-25T16:43:52Z Revision history for this page on the wiki MediaWiki 1.27.7 https://steamforge.net/wiki/index.php?title=How_To_set_a_Python_matplotlib_axis_to_display_from_-Pi_to_Pi&diff=1497&oldid=prev Sean: Created page with "[https://www.python.org/ Python] and [https://matplotlib.org/ Matplotlib] are great tools for doing Science! In particular, I like to use the [https://www.spyder-ide.org/ Spyd..." 2020-05-08T16:06:24Z <p>Created page with &quot;[https://www.python.org/ Python] and [https://matplotlib.org/ Matplotlib] are great tools for doing Science! In particular, I like to use the [https://www.spyder-ide.org/ Spyd...&quot;</p> <p><b>New page</b></p><div>[https://www.python.org/ Python] and [https://matplotlib.org/ Matplotlib] are great tools for doing Science! In particular, I like to use the [https://www.spyder-ide.org/ Spyder IDE]. Matplotlib makes it easy to create graphs from datasets. In the world of [https://en.wikipedia.org/wiki/Digital_signal_processing DSP], an example of this usage is plotting sampled data sets in the time domain and frequency domain. However, by default your frequency domain plot's x-axis will simply be labelled with the indices of your sampled data array. In DSP it is often desirable to remap this range of indices (0 to N-1) to the frequencies of -Pi to Pi. Unfortunately there isn't a 1 step solution to this remapping. However, with a little bit of code you can accomplish this relatively easily.<br /> <br /> The code below with show you how to go from this most basic graph of a DFT:<br /> <br /> [[File:dft.png]]<br /> <br /> to the much, prettier, shifted and relabeled graph of a DFT<br /> <br /> [[File:Shifted_dft_-pi_to_pi.png]]<br /> <br /> To remap your DFT so that it is centered about 0 frequency you can use the following &lt;code&gt;dft_shift&lt;/code&gt; function.<br /> <br /> &lt;syntaxhighlight lang=&quot;python&quot; line='line'&gt;<br /> import matplotlib<br /> import matplotlib.pyplot as plt<br /> import numpy as np<br /> <br /> # define dft_shift to center the DFT about 0. Make sure the final length is odd so 0 is at the center<br /> def dft_shift(X):<br /> N = len(X)<br /> if (N % 2 == 0):<br /> # even-length: return N+1 values<br /> return np.arange(-int(N/2), int(N/2) + 1), np.concatenate((X[int(N/2):], X[:int(N/2)+1]))<br /> else:<br /> # odd-length: return N values<br /> return np.arange(-int((N-1)/2), int((N-1)/2) + 1), np.concatenate((X[int((N+1)/2):], X[:int((N+1)/2)]))<br /> &lt;/syntaxhighlight&gt;<br /> <br /> To change a matplotlib plot's x-axis to display from -Pi to Pi you can use the following lines of code. X is assumed to be your sampled data array<br /> &lt;syntaxhighlight lang=&quot;python&quot; line='line'&gt;<br /> n_pi = np.linspace(-np.pi,np.pi,len(X)) # create array from -pi to pi that is the same length as your sampled data array<br /> plt.xlim([-np.pi, np.pi]) # set x-axis display range from -Pi to Pi<br /> plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],<br /> [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$\pi$']) # display ticks in pi format rather than 3.14 format<br /> plt.stem(n_pi, abs(X)) # plot sampled data array X using n_pi as the indicies<br /> &lt;/syntaxhighlight&gt;<br /> <br /> The complete Python script below puts it all together and walks you through graphing the same data in various formats.<br /> <br /> &lt;syntaxhighlight lang=&quot;python&quot; line='line'&gt;<br /> import matplotlib<br /> import matplotlib.pyplot as plt<br /> import numpy as np<br /> <br /> # define dft_shift to center the DFT about 0. Make sure the final length is odd so 0 is at the center<br /> def dft_shift(X):<br /> N = len(X)<br /> if (N % 2 == 0):<br /> # even-length: return N+1 values<br /> return np.arange(-int(N/2), int(N/2) + 1), np.concatenate((X[int(N/2):], X[:int(N/2)+1]))<br /> else:<br /> # odd-length: return N values<br /> return np.arange(-int((N-1)/2), int((N-1)/2) + 1), np.concatenate((X[int((N+1)/2):], X[:int((N+1)/2)]))<br /> <br /> # define the size of the figures<br /> plt.rcParams[&quot;figure.figsize&quot;] = (14,4)<br /> <br /> # create simple baseband signal<br /> N = 64<br /> x = np.ones(N)<br /> <br /> # plot x<br /> plt.figure()<br /> plt.title(&quot;baseband signal of 1s&quot;)<br /> plt.stem(x)<br /> <br /> # plot DFT(x) = X<br /> X = np.fft.fft(x)<br /> plt.figure()<br /> plt.title(&quot;basic DFT&quot;)<br /> plt.stem(abs(X))<br /> <br /> # plot shifted X with default indicies<br /> n,X_shift = dft_shift(X) # get the shifted version of X. n contains integer indicies centered about 0<br /> plt.figure()<br /> plt.title(&quot;shifted DFT with default indicies&quot;)<br /> plt.stem(abs(X_shift)) # plot shifted X with &quot;default&quot; 0 to len(X_shift) indicies<br /> <br /> # plot shifted X with n as indicies<br /> n,X_shift = dft_shift(X) # get the shifted version of X. n contains integer indicies centered about 0<br /> plt.figure()<br /> plt.title(&quot;shifted DFT with n as indicies&quot;)<br /> plt.stem(n, abs(X_shift)) # plot shifted X using n as indicies<br /> <br /> # plot shifted X with plot scaled from -Pi to Pi (but still with integer indicies)<br /> n,X_shift = dft_shift(X) # get the shifted version of X. n contain indicies centered about 0<br /> plt.figure()<br /> plt.title(&quot;shifted DFT from -Pi to Pi with n as indicies (notice only 7 points are plotted)&quot;)<br /> plt.xlim([-np.pi, np.pi]) # x-axis display range from -Pi to Pi<br /> plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],<br /> [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$\pi$']) # display ticks in pi format rather than 3.14 format<br /> plt.stem(n, abs(X_shift)) # plot shifted X using n as the indicies<br /> <br /> # plot shifted X with X-axis indicies from -Pi to Pi and plot scaled from -Po to Pi<br /> n,X_shift = dft_shift(X) # get the shifted version of X. n contain indicies centered about 0<br /> n_pi = np.linspace(-np.pi,np.pi,len(n)) # create array from -pi to pi that is the same length as n<br /> plt.figure()<br /> plt.title(&quot;shifted DFT from -Pi to Pi with n_pi as indicies&quot;)<br /> plt.xlim([-np.pi, np.pi]) # x-axis display range from -Pi to Pi<br /> plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],<br /> [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$\pi$']) # display ticks in pi format rather than 3.14 format<br /> plt.stem(n_pi, abs(X_shift)) # plot shifted X using n_pi as the indicies<br /> &lt;/syntaxhighlight&gt;<br /> <br /> =REFERENCES=<br /> *Coursera Digital Signal Process [https://www.coursera.org/learn/dsp/ Course] developed by the [https://www.epfl.ch/en/ EPFL]<br /> *Rougier's Matplotlib [https://github.com/rougier/matplotlib-tutorial Tutorial]</div> Sean