0

[Python 2.7.12]

[Matplotlib 1.5.1]

Every scan cycle my code produces a 'top' score. I want to plot the performance over time. I have simplified the code into the example below:

import matplotlib.pyplot as plt
from matplotlib import lines
import random
count = 1

plt.axis([0, 1000, 0, 100])
plt.ion()

while True:
    count += 1
    a=random.randint(1, 50)
    plt.plot(count, a,'xb-')
    plt.pause(0.05)

plt.show()

My aim is to produce a line graph. The problem is whatever I set the line style to, it doesn't take effect. It only plots scatter type graphs. I am however able to change whether its a dot or an 'X' marker.

Or is the problem with the fact that the scores are 'plot and forget' so it has nothing to draw from?

EDIT: Plotting will be done in real-time

3
  • are you trying to do animation, or you just need a line graph? Commented Sep 29, 2017 at 17:26
  • Real-time plotting (Edited original question). Commented Sep 29, 2017 at 17:33
  • 1
    I am not sure about the solution, but the problem is that a and count are scalars, so when plot is called, it is plotting a single point, and calling plot multiple times won't connect the dots, because it will generate a different line instance, Commented Sep 29, 2017 at 17:34

1 Answer 1

2

You need at least 2 points to draw a line. You can store and use the previous state in each step.

import matplotlib.pyplot as plt
from matplotlib import lines
import random

x = 1

plt.axis([0, 1000, 0, 100])
plt.ion()

y_t1 = random.randint(1, 50)
plt.plot(1, y_t1, 'xb')
plt.pause(0.05)

while True:
    x += 1
    y_t2 = random.randint(1, 50)
    plt.plot([x - 1, x], [y_t1, y_t2], 'xb-')
    y_t1 = y_t2
    plt.pause(0.05)

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.