matlab 的 Python 的“坚持”替代方案

时间:2021-08-01 18:47:48

标签: python numpy matplotlib plot imshow

我有一些名为 group 的数据集,我想将它们绘制在一个空图上。我的意思是当计算机读取新数据集时,我的程序会将其添加到图形中。

for groups in seconds_list:
    group = f[(groups)][:]

    i_data = group['real']
    q_data = group['imag']

    i_data = np.array(i_data, dtype=float64)
    q_data = np.array(q_data, dtype=float64)    

    # 10.log(10.(I^2+Q^2)+1) then rotate array w/ np.rot90()
    power_ch1 = np.rot90(np.array(np.log10(((np.add(np.square(np.abs(i_data[:, ::2])) , np.square(np.abs(q_data[:, ::2]))))*10)+1)*10 , dtype=float64))
    
    print("Group: ", groups)

    fig, ax = plt.subplots()
    ax.set_xlim(0, 475*200)
    ax.set_ylim(0, 3000)

    tx, ty = my_list[i]
    ax.imshow(power_ch1, cmap='viridis', interpolation='nearest', aspect='auto', extent = (tx, tx+200, ty, ty+2700))
    i +=1
    plt.tight_layout()
    plt.show()

我正在使用此代码,但我无法保留旧图。我想在旧数据集旁边添加新数据集。我该怎么做?我在 matlab 中使用 hold on,但找不到 Python 的替代品。

1 个答案:

答案 0 :(得分:1)

您可以使用 matlab 样式而不是 object 样式(通过执行 fig, ax = plt.subplots() 使用)来实现,如下例所示:

import matplotlib.pyplot as plt

x = np.linspace(1., 10., 100)
y1 = np.log(x)
plt.plot(x, y1, color='r', marker='*', label='log(x)')

y2 = np.sin(x)
plt.plot(x, y2, color='b', marker='^', label='sin(x)')

plt.legend()
plt.xlabel('x')
plt.ylabel('y(x)')
plt.show()

输出

enter image description here

您可以找到有关 matplotlib in this great book 绘图功能的更多信息。

干杯