現在我的 x 軸上有 9 個小刻度(我認為是 xticks)。我需要將該數字調整為 4 個小刻度,因為我的分配要求是方波圖的精確再現。這對我來說似乎是一個小細節,因為資料在那里并且是正確的,但是如果它不完全匹配,我會得到零。
這是我的代碼:
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()
uj5u.com熱心網友回復:
干得好:
import matplotlib.ticker
ax0.xaxis.set_minor_locator(ticker.AutoMinorLocator(5))
這將是您的代碼:
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()

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/334213.html
標籤:Python 麻木 matplotlib 阴谋 scipy
