Right now I have 9 minor ticks (xticks, I think) on my x-axis. I need to adjust that number to 4 minor ticks because my assignement requirememt is an exact reporoduction of a square wave graph. It seems a petty detail to me because the data is there and correct, but if it doesn't match exactly I get a zero.
Here is my code:
from matplotlib import pyplot as plt
import matplotlib.ticker as ticker
from matplotlib.ticker import FormatStrFormatter
from scipy import fftpack
freq = 440
period = 2/freq
time_step = period/40 #sampling interval
time_vec = np.arange(0, 4*period,time_step)
sig1 = (np.cos(2 * np.pi * time_vec / period))
sig2 = (np.cos(2 * np.pi * time_vec * 3 / period))/3
sig3 = (np.cos(2 * np.pi * time_vec * 5 / period))/5
sig4 = (np.cos(2 * np.pi * time_vec * 7 / period))/7
sig5 = (np.cos(2 * np.pi * time_vec * 9 / period))/9
sig6 = (np.cos(2 * np.pi * time_vec * 11 / period))/11
sig7 = (np.cos(2 * np.pi * time_vec * 13 / period))/13
sig8 = (np.cos(2 * np.pi * time_vec * 15 / period))/15
sig9 = (np.cos(2 * np.pi * time_vec * 17 / period))/17
sig10 = (np.cos(2 * np.pi * time_vec * 19 / period))/19
sig = sig1-sig2 sig3-sig4 sig5-sig6 sig7-sig8 sig9-sig10
x = sig
def plotSignal(theTimeVec,theData,theLabel):
fig,ax0 = plt.subplots(1,1,
facecolor='0.75',
linewidth=3,
edgecolor='Black')
ax0.plot(theTimeVec,theData,label=theLabel)
ax0.legend(loc='center left',framealpha=0.3,facecolor='Green')
if theLabel == 'none':
ax0.legend_.remove()
ax0.set_xlabel('Time in seconds')
ax0.grid(True)
freq_interval = (theTimeVec[1])
ax0.xaxis.set_major_locator(ticker.MultipleLocator(20*time_step))
ax0.xaxis.set_minor_locator(ticker.MultipleLocator(2*time_step))
ax0.tick_params(axis='both',which='minor',length=5)
ax0.tick_params(axis='both',which='major',color='black',
length=10,labelcolor='blue',width=2)
ax0.xaxis.set_major_formatter(FormatStrFormatter('%.3f'))
plotSignal(time_vec,sig,
'220 Hz square wave - myname')
plt.savefig('SquareWave.jpg')
plt.show()
CodePudding user response:
Here you go:
import matplotlib.ticker
ax0.xaxis.set_minor_locator(ticker.AutoMinorLocator(5))
Here would be your code:
from matplotlib import pyplot as plt
import matplotlib.ticker as ticker
from matplotlib.ticker import FormatStrFormatter
from scipy import fftpack
freq = 440
period = 2/freq
time_step = period/40 #sampling interval
time_vec = np.arange(0, 4*period,time_step)
sig1 = (np.cos(2 * np.pi * time_vec / period))
sig2 = (np.cos(2 * np.pi * time_vec * 3 / period))/3
sig3 = (np.cos(2 * np.pi * time_vec * 5 / period))/5
sig4 = (np.cos(2 * np.pi * time_vec * 7 / period))/7
sig5 = (np.cos(2 * np.pi * time_vec * 9 / period))/9
sig6 = (np.cos(2 * np.pi * time_vec * 11 / period))/11
sig7 = (np.cos(2 * np.pi * time_vec * 13 / period))/13
sig8 = (np.cos(2 * np.pi * time_vec * 15 / period))/15
sig9 = (np.cos(2 * np.pi * time_vec * 17 / period))/17
sig10 = (np.cos(2 * np.pi * time_vec * 19 / period))/19
sig = sig1-sig2 sig3-sig4 sig5-sig6 sig7-sig8 sig9-sig10
x = sig
def plotSignal(theTimeVec,theData,theLabel):
fig,ax0 = plt.subplots(1,1,
facecolor='0.75',
linewidth=3,
edgecolor='Black')
ax0.plot(theTimeVec,theData,label=theLabel)
ax0.legend(loc='center left',framealpha=0.3,facecolor='Green')
if theLabel == 'none':
ax0.legend_.remove()
ax0.set_xlabel('Time in seconds')
ax0.grid(True)
freq_interval = (theTimeVec[1])
ax0.xaxis.set_major_locator(ticker.MultipleLocator(20*time_step))
ax0.xaxis.set_minor_locator(ticker.MultipleLocator(2*time_step))
ax0.tick_params(axis='both',which='minor',length=5)
ax0.tick_params(axis='both',which='major',color='black',
length=10,labelcolor='blue',width=2)
ax0.xaxis.set_major_formatter(FormatStrFormatter('%.3f'))
import matplotlib.ticker
ax0.xaxis.set_minor_locator(ticker.AutoMinorLocator(5))
plotSignal(time_vec,sig,
'220 Hz square wave - myname')
plt.savefig('SquareWave.jpg')
plt.show()