【问题标题】:adding time ticks for x axis of a RADAR data为雷达数据的 x 轴添加时间刻度
【发布时间】:2021-05-21 20:30:29
【问题描述】:

我有两个 NumPy 数组 (2700,1000),用于将 5 秒的雷达数据绘制为通道 1 和通道 2。2700 (m) 是范围,每 200 个 x 数表示一秒。

但我想每 2 秒向 x 轴添加一次刻度。 power_ch1 和 power_ch2 我上面提到的两个不同的数组。

csfont = {'fontname':'Times New Roman'}                       # font applied as 'Times New Roman'

fig, ax = plt.subplots()

plt.subplot(211)                                              # Channel 1 located
plt.imshow(power_ch1, cmap='viridis',                         # Channel 1 is plotted with [plt.imshow()], colormap is 'viridis'
           interpolation='nearest',                           
           aspect='auto')                                     # 475 number is time(s) and 405 number is range(km)
plt.title(r'Reveiver Channel A',**csfont)                     # plot title is written and font has been set as 'Times New Roman'
plt.ylabel('RANGE (km)',**csfont)                             # Text('RANGE') has been printed on the y-axis
plt.xlabel('LOCAL TIME (hh:mm:ss)',**csfont)                  # Text('LOCAL TIME (hh:mm:ss)') has been printed on the x-axis
plt.xticks(x, array_clock)                                   # ticks set on x-axis and 'array_clock' printed here
plt.yticks(y)                                                # ticks set on y-axis
cbar = plt.colorbar()                                         # colorbar located
cbar.set_label('POWER (dB)',**csfont)                         # 'POWER (dB)' text printed next to colorbar

plt.subplot(212)                                              # Channel 2 located

plt.imshow(power_ch2, cmap='viridis',                         # Channel 2 is plotted with [plt.imshow()], colormap is 'viridis' 
           interpolation='nearest', 
           aspect='auto')                                     # 475 number is time(s) and 405 number is range(km)
plt.title(r'Reveiver Channel B',**csfont)                     # plot title is written and font has been set as 'Times New Roman'
plt.ylabel('RANGE (km)',**csfont)                             # Text('RANGE') has been printed on the y-axis
plt.xlabel('LOCAL TIME (hh:mm:ss)',**csfont)                  # Text('LOCAL TIME (hh:mm:ss)') has been printed on the x-axis
plt.xticks(x, array_clock)                                   # ticks set on x-axis and 'array_clock' printed here
plt.yticks(y)                                                # ticks set on y-axis
cbar = plt.colorbar()                                         # colorbar located
cbar.set_label('POWER (dB)',**csfont)                         # 'POWER (dB)' text printed next to colorbar

fig.tight_layout()                                            # tight_layout automatically adjusts subplot params so that the
                                                              # subplot(s) fits in to the figure area.

plt.show()                                                    # Display all open figures.

【问题讨论】:

    标签: python numpy matplotlib time imshow


    【解决方案1】:

    你可以将你的刻度替换为默认的,只计算原始数组中的像素……

    len_sec, len_pix = 5, 2700 
    pix_sec = (len_pix-1)/len_sec # len_pix-1 to account for fence effect
    delta_sec = 2 # tick spacing
    xs = np.arange(0, len_pix, pix_sec*delta_sec)
    plt.gca().set_xticks(xs)
    plt.gca().set_xticklabels(["%.2f"%(x/pix_sec) for x in xs])
    

    注意1

    您可能希望对 y 轴执行类似操作,并且可能希望反转其方向,底部为 0

    plt.gca().invert_yaxis()
    

    注意2

    因为你要使用 Axis 对象的方法,所以可能你 想要ax1 = plt.add_subplot(...) 以避免重复出现plt.gca()。

    注意3

    您可能想要进行循环以避免大量代码重复,并降低在两个子图中进行不同更改的风险。


    我决定实施我的所有建议,请参见顶部的图,我使用以下方法生成:

    import numpy as np
    import matplotlib.pyplot as plt
    
    # ## ### 4444444 ♥ for cascadia ### ## #
    X, Y = 2700, 1000
    
    duration_s = 5
    length_km = 5 # I don't really know
    pixel_s = X/duration_s
    pixel_km = Y/length_km
    delta_s = 1.0
    delta_km = 1.50
    
    xs = np.arange(0, X+1, delta_s*pixels_s)
    ys = np.arange(0, Y+1, delta_km*pixel_km)
    
    data = np.arange(X*Y).reshape(Y, X)/X/Y*10 # 0<= data[i,j] < 10
    channel = iter(['A', 'B'])
    
    fig, axes = plt.subplots(
        2, 1,
        figsize=(8, 6), dpi=100,
        constrained_layout=1)
    
    for i, ax in enumerate(axes):
        image = ax.imshow(data)
        ax.set_title(r'Receiver Channel ' + next(channel))
        ax.set_ylabel('RANGE (km)'); ax.invert_yaxis()
        ax.set_xlabel('LOCAL TIME (hh:mm:ss)')
        for axis,vec,pix in zip((ax.xaxis,ax.yaxis),(xs,ys),(pixel_s,pixel_km)):
            axis.set_ticks(vec)
            axis.set_ticklabels(["%.2f"%(v/pix) for v in vec])
        cbar = plt.colorbar(image, ax=ax)
        cbar.set_label('POWER (dB)')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 2020-07-29
      相关资源
      最近更新 更多