1

I am trying to use plt.scatter to generate multiple points and I want to connect each point with the previous one. For my x-axes I need to use the format time.time() or something that will allow me to draw points each second.

I tried to use plt.plot(), but that will result in changes I don't need.

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import time

ts = time.time()
kraft = 2300

for i in range(10):
    ts1 = ts + i
    kraft1 = kraft + i

    plt.scatter(ts1, kraft1)

plt.show()

I expect to have multiple points connected to the former point.

Thanks for you answers.

Gan Zo
  • 25
  • 4
  • i think this was answered here: https://stackoverflow.com/questions/40516661/adding-line-to-scatter-plot-using-pythons-matplotlib – Itamar Mushkin Jul 11 '19 at 09:05
  • Possible duplicate of [Adding line to scatter plot using python's matplotlib](https://stackoverflow.com/questions/40516661/adding-line-to-scatter-plot-using-pythons-matplotlib) – Itamar Mushkin Jul 11 '19 at 09:05
  • I am sorry if I am misunderstading, but in this example the line acts independetly from the scatter points. I want to create scatter points and the following point should connect via line with the previous one. – Gan Zo Jul 11 '19 at 09:25

1 Answers1

2

The straightforward solution is to use save your values in a list and plot all of them at once using style '-o' which represents a line and a marker. You don't need extra variables ts1 and kraft1 here

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import time

ts = time.time()
kraft = 2300

x, y  = [], []
for i in range(10):
    x.append(ts + i)
    y.append(kraft + i)

plt.plot(x, y, '-o')

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • First of all thanks for your answer and sorry for my broken english. https://stackoverflow.com/questions/56966939/how-to-draw-live-data-with-time-passed-in-milliseconds-on-x-axes. I posted here my original question before I decided to reduce the code to the basics, but do you think I can use the same method here? – Gan Zo Jul 11 '19 at 09:43
  • @GanZo : You can try appending to list in the `if` part and then plot using the above commands in the `else` part. – Sheldore Jul 11 '19 at 11:46