1

I want to plot several lines in a figure. All points of each line aren't available at the beginning, they become available gradually.

I want to plot something like this

Whole points for each line aren't available at the beginning, for example at t=3 points for a line is [(1,0),(2,3), (3,6)] and in t=4 a new point comes [(1,0),(2,3), (3,6), (4, 9)].

To be more clear, I don't have all points for drawing, point are given gradually. Therefore, I need to constantly update my plot in order to catch changes.

CVDE
  • 393
  • 3
  • 19

2 Answers2

1

A picture like this can be plotted with:

from __future__ import division
import matplotlib.pyplot as plt
from random import randint  

data = [ 
    ("1 line", [randint(0, 10) / 10 for i in range(20)]),
    ("2 line", [randint(0, 10) / 10 for i in range(20)]),
    ("3 line", [randint(0, 10) / 10 for i in range(20)]),
    ("4 line", [randint(0, 10) / 10 for i in range(20)])
]

for label, y in data:
    plt.plot(y, label = label)
plt.legend()
plt.show()
quant
  • 2,184
  • 2
  • 19
  • 29
  • Thanks for the code. But it's not my question, whole points for each line aren't available at the beginning, for example at t=3 points for a line is [(1,0),(2,3), (3,6)] and in t=4 a new point comes [(1,0),(2,3), (3,6), (4, 9)]. I can plot one line in this situation. But I have no Idea for several lines. – CVDE Mar 09 '18 at 06:42
0

You have to update matplotlib artist data each time you receive new values. At first you plot line with no data like:

l = plt.plot([], [], 'r-')[0]

and later update data with

l.set_data(ydata, xdata)

Don't forget redraw with

plt.draw()

If you have more lines, plot function returns list of artists, so each need to be updated.

Ondro
  • 997
  • 5
  • 8