Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving x-axis in matplotlib during real time plot (python)

I want to manipulate the x-axis during a real time plot so that at most a number of 10 samples are seen at a time. It seems like plt.axis() updates just once after the plot has been initialized. Any suggestions? Thanks in advance!

import numpy as np
import matplotlib.pyplot as plt

# Initialize
x_axis_start = 0
x_axis_end = 10

plt.axis([x_axis_start, x_axis_end, 0, 1])
plt.ion()

# Realtime plot
for i in range(100):
    y = np.random.random()
    plt.scatter(i, y)
    plt.pause(0.10)
    # print(i)

    if i%10 == 0 and i>1:
        # print("Axis should update now!")
        plt.axis([x_axis_start+10, x_axis_end+10, 0, 1])
like image 657
NumbThumb Avatar asked Aug 25 '16 08:08

NumbThumb


1 Answers

You have to update x_axist_start and x_axis_end in the if statement!

if i%10 == 0 and i>1:
    print("Axis should update now!")
    x_axis_start += 10
    x_axis_end += 10
    plt.axis([x_axis_start, x_axis_end, 0, 1])

This does the trick! :)

Explanation: You only added 10 once to both parameters. In the end you always added 10 to 0 and 10, leaving you with only one update.

like image 141
Ian Avatar answered Nov 15 '22 04:11

Ian