【问题标题】:how to adjust number of minor ticks如何调整次要刻度的数量
【发布时间】:2021-12-09 06:58:47
【问题描述】:

现在我的 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()

【问题讨论】:

    标签: python numpy matplotlib plot scipy


    【解决方案1】:

    给你:

    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()
    

    【讨论】:

    • 你是最棒的。谢谢。如果我可以投票,我会的!
    • @homerwight - 您可以单击左侧的复选标记以接受答案:-)
    猜你喜欢
    • 2018-06-03
    • 2014-06-03
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 2020-03-11
    • 2015-09-02
    相关资源
    最近更新 更多