0

I am new to this module. I have time series data for movement of particle against time. The movement has its X and Y component against the the time T. I want to plot these 3 parameters in the graph. The sample data looks like this. The first column represent time, 2nd- Xcoordinate , 3rd Y-coordinate.

1.5193      618.3349        487.5595    
1.5193      619.3349        487.5595    
2.5193      619.8688        489.5869    
2.5193      620.8688        489.5869    
3.5193      622.9027        493.3156    
3.5193      623.9027        493.3156    
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • The time values appear to be getting duplicated for different position values, (i.e. 1.5193 corresponds to x position 618.3349 **and** 619.3349)... how are you collecting/generating this data? – William Miller Jan 23 '20 at 20:20
  • Hi William, I am collecting the data from a Simulator Omnet++ and SUMO. The timestamp is same but the X cordinate changes as the vehicles move at high speed. – pradeep_88 Jan 24 '20 at 04:38
  • You understand that the velocity between the first and second timestamp is at least 1000, but the velocity between the second and third timestamp is at most 0.5. That is an acceleration of magnitude 999.5 at least. You’re saying this is simulating a vehicle? If the units here are meters and seconds then it’s pulling more than 100 gs. I can help you figure out the plotting but I would suggest you examine your simulation. The results here appear non-physical – William Miller Jan 24 '20 at 04:48
  • Hi William, I am not sure how much the data is realistic, I am running in the express mode, might be because of this the data looks unrealistic. – pradeep_88 Jan 24 '20 at 05:11
  • Well if you’re okay having multiple data points at each time then this would be pretty easy to do using a set of 3D axes. Would that be acceptable? – William Miller Jan 24 '20 at 08:35
  • Yes. But William I have a doubt, as how to plot the graph with continuous data coming? – pradeep_88 Jan 24 '20 at 08:54
  • The data is continuously streaming from simulator, so how to plot the graph with this data? – pradeep_88 Jan 24 '20 at 08:55
  • That simply means this will require a method for updating the plot on a loop... is the data being stored in a file that is getting updated with the new data or just an array of some kind? – William Miller Jan 24 '20 at 08:59
  • The data is getting stored in the file and the new data keeps appending . Rather I want the graph plotting from the data coming directing from the simulator. – pradeep_88 Jan 24 '20 at 09:29
  • William, the question you had asked about the units of X and Y. Those are in milimeters. – pradeep_88 Jan 24 '20 at 09:30
  • Okay, that shouldn’t be too difficult - I haven’t the time at the moment to put together an example but I will do so within the next few days. Feel free to ping me if I haven’t posted an answer in the next week or so.... also millimeters makes much more sense physically - changing from 1 m/s to ~0 m/s in 1s is much more reasonable than 1000 m/s to ~0 m/s in 1s – William Miller Jan 24 '20 at 09:33
  • William can you let me know any resource I can refer for dynamic plotting the graph direct from simulator?? I ll too start exploring it – pradeep_88 Jan 24 '20 at 09:37
  • Assuming you already have a way to read the data in from the file the method’s in [this answer](https://stackoverflow.com/a/59076620/10659910) could be employed to perform the updating - you would then need to call the update function in a loop of some sort while you accumulated data from the simulation – William Miller Jan 24 '20 at 09:40
  • Okay William , sure – pradeep_88 Jan 24 '20 at 09:45
  • @WilliamMiller _"the velocity between the first and second timestamp is at least 1000, but the velocity between the second and third timestamp is at most 0.5"_ Maybe they are simulating a crash test... – gboffi Jan 24 '20 at 10:19
  • 1
    @gboffi Turns our the units are millimeters, not meters (note I did say *if the units here are meters* in the question you quote). A vehicle crash could indeed produce such a massive deceleration of 1000 m/s/s - but the subsequent acceleration of 1000 m/s/s (between third and fourth timestamp) would not be explained by such circumstance. – William Miller Jan 24 '20 at 10:24

1 Answers1

0

If you want to add a 3rd info to a 2D curve, one possibility is to use a color mapping instituting a relationship between the value of the 3rd coordinate and a set of colors.

In Matplotlib we have not a direct way of plotting a curve with changing color, but we can fake one using matplotlib.collections.LineCollection.

In the following I've used some arbitrary curve but I have no doubt that you could adjust my code to your particular use case if my code suits your needs.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

# e.g., a Lissajous curve
t = np.linspace(0, 2*np.pi, 6280)
x, y = np.sin(4*t), np.sin(5*t)

# to use LineCollection we need an array of segments
# the canonical answer (to upvote...) is https://stackoverflow.com/a/58880037/2749397
points = np.array([x, y]).T.reshape(-1,1,2)
segments = np.concatenate([points[:-1],points[1:]], axis=1)

# instantiate the line collection with appropriate parameters,
# the associated array controls the color mapping, we set it to time

lc = LineCollection(segments, cmap='nipy_spectral', linewidth=6, alpha=0.85)
lc.set_array(t)

# usual stuff, just note ax.autoscale, not needed here because we
# replot the same data but tipically needed with ax.add_collection

fig, ax = plt.subplots()
plt.xlabel('x/mm') ; plt.ylabel('y/mm')
ax.add_collection(lc)
ax.autoscale()

cb = plt.colorbar(lc)
cb.set_label('t/s')

# we plot a thin line over the colormapped line collection, especially
# useful when our colormap contains white...
plt.plot(x, y, color='black', linewidth=0.5, zorder=3)

plt.show()

enter image description here

gboffi
  • 22,939
  • 8
  • 54
  • 85
  • Hi Gboffi, How can I plot the live graph from the the data streaming continuosly. – pradeep_88 Jan 28 '20 at 06:51
  • As far as I can tell, my post answers [the question you've asked](https://stackoverflow.com/q/59875363/2749397). If you have a different problem now, I'd suggest asking a different question. – gboffi Jan 28 '20 at 12:12