Shifitng and moving curve matplotlib

53 views Asked by At

So I am trying to make the curve moves as shown in the link, I have thought about moving limits each time but the graph is still static. I have tried before that its starts plot and pause but it wasn't like as shown smooth. https://drive.google.com/file/d/1sBunkQT-8gB2ttCLAmoYQaniO-qUp6in/view

limit_values = [] 
limity_values = []

for value in data[1:]:
    
    
    x = float(value[0])
    y = float(value[1])
    limit_values.append(x)
    limity_values.append(y)


time_values=[]
amplitude_values=[]
counter_for_changing_limit=0
limits_changer_x=[]
limits_changer_y=[]

for value in data[1:]:
    x = float(value[0])
    y = float(value[1])
    time_values.append(x)
    amplitude_values.append(y)
    limits_changer_x.append(x)
    limits_changer_y.append(y)
    plt.xlim(min(limits_changer_x), max(limits_changer_x))
    plt.ylim(min(limity_values), max(limity_values))
    counter_for_changing_limit+=1
    # if(counter_for_changing_limit==20 or counter_for_changing_limit==40 or counter_for_changing_limit==60):
    #     plt.plot(time_values, amplitude_values, color='black',label="signal one")
    #     plt.pause(0.1)
    
    
    if(counter_for_changing_limit==60):
        counter_for_changing_limit=0
        del limits_changer_x[0:1]
        del limits_changer_y[0:1]
    plt.plot(limit_values, limity_values, color='black',label="signal one")
    
plt.title('Signal Viewer')  
plt.xlabel('Time (s)')  
plt.ylabel('Amplitude (m)')  
plt.show()
1

There are 1 answers

2
tetris programming On

There are some things missing or on the wrong place in the example above. Compare it with the code on the bottom.

Here some explanations:

added [-1:] because you dont want to recalculate your entire plot for every step

plt.plot(limit_values[-1:], limity_values[-1:], color='black', label="signal one")

Put your xlim inside your if loop and set the xlim to the range you want to see:

    plt.plot(time_values, amplitude_values, color='black', label="signal one")
    plt.xlim(max(limits_changer_x) - 20, max(limits_changer_x))
    #used -20 to show the last 20 time values
    plt.ylim(min(limity_values), max(limity_values))
    plt.pause(0.1)

This is how it looks for your example:

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(200)
data = np.hstack([np.arange(0, 500).reshape(500, 1), np.random.randint(600, size=(500), ).reshape(500, 1)])
print(data)

limit_values = []
limity_values = []

for value in data[1:]:
    x = float(value[0])
    y = float(value[1])
    limit_values.append(x)
    limity_values.append(y)

time_values = []
amplitude_values = []
counter_for_changing_limit = 0
limits_changer_x = []
limits_changer_y = []

for value in data[1:]:
    x = float(value[0])
    y = float(value[1])
    time_values.append(x)
    amplitude_values.append(y)
    limits_changer_x.append(x)
    limits_changer_y.append(y)

    counter_for_changing_limit += 1
    if (counter_for_changing_limit == 20 or counter_for_changing_limit == 40 or counter_for_changing_limit == 60):
        plt.plot(time_values, amplitude_values, color='black', label="signal one")
        plt.xlim(max(limits_changer_x) - 20, max(limits_changer_x))
        plt.ylim(min(limity_values), max(limity_values))
        plt.pause(0.1)

    if (counter_for_changing_limit == 60):
        counter_for_changing_limit = 0
        del limits_changer_x[0:1]
        del limits_changer_y[0:1]
    plt.plot(limit_values[-1:], limity_values[-1:], color='black', label="signal one")

plt.title('Signal Viewer')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude (m)')
plt.show()