matlab's "hold on" alternative for python

Viewed 557

I have some datasets named group and I want to plot them on a empty figure. I mean when new dataset read by computer, my program will add it to figure.

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

I am using this code but I can't hold the old plots. I want to add new dataset next to old one. How can I do it? I was using hold on with matlab but I couldn't find an alternative for python.

1 Answers

You can do it by using the matlab style instead of the object style (which you use by executing the fig, ax = plt.subplots()), as is shown in the following example:

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

Output

enter image description here

You can find more information on the plotting capabilities of matplotlib in this great book.

Cheers

Related