0

I am dealing with a data frame as shown below. x and y are the Mercator x, y coordinates of trajectories with time. Rows with same VoyageIDs are the points belonging to the same trajectory. As we can see the rows with all 0's are separating different trajectories.

        VoyageID      X             Y             Time
     27 2             -7.35857534   2.09175178     1.29471228
     28 2             -7.35863779   2.09167080     1.29471234
     29 2             -7.35882203   2.09156224     1.29471240
     30 2             -7.35908808   2.09147633     1.29471246
     31 2             -7.35941313   2.09134900     1.29471252
     32 2             -7.35970112   2.09123810     1.29471258
     33 0             0.0000000     0.0000000      0.0000000
     34 3             -7.34769342   2.09628155     1.29498270
     35 3             -7.34811254   2.09626864     1.29498282
     36 3             -7.34853711   2.09625315     1.29498288
     37 3             -7.34889255   2.09622732     1.29498294
     38 0             0.0000000     0.0000000      0.0000000
     39 4             -7.35857089   2.09176469     1.29531606
     40 4             -7.35862989   2.09169697     1.29531612
     41 4             -7.35869312   2.09162679     1.29531618
     42 4             -7.35876692   2.09158959     1.29531624
     43 0             0.0000000     0.0000000      0.0000000

I would kindly request to suggest me the best way to visualize these trajectories in the following two ways:

  1. How can I plot x, y coordinates on a simple 2d line plot ?

  2. How can I plot the trajectories with x,y coordinates on a leaflet map (Using Folium or any other real map) ?

Also, how do I manage the points of different trajectories (They are separated by 0's). I am new to python as well as matplotlib, so please provide me with little detailed answers if possible. Thanks in advance.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Liza
  • 961
  • 3
  • 19
  • 35
  • @roganjosh I am sorry, I meant only x, y coordinates on the map, not the time element. I have edited the question. – Liza Mar 20 '17 at 18:31
  • It's still a huge question. The Leaflet docs specify how to add markers to a map [here](http://leafletjs.com/reference.html#marker) so you just need to put that into a JS `for` loop, at least on the fundamental level. – roganjosh Mar 20 '17 at 18:36
  • You need to decide which library to use. You are talking about leaflet which is JavaScript, but you tagged your question with python. It's also not clear if you need a real map in the background or just a coordinate plot. The role of time is unclear, since there is no time on a map. – ImportanceOfBeingErnest Mar 20 '17 at 18:36
  • And as @ImportanceOfBeingErnest has stated better than I did, this is a JS library but you tagged in Python. In my case, I'm trying to use Django to communicate the data but you haven't said how yours gets from Python to JavaScript in your setup. You underestimate what you are asking here, for sure. – roganjosh Mar 20 '17 at 18:39
  • @ImportanceOfBeingErnest I am sorry, for the confusions. I am working in python, and I guess we can use Folium to work on leaflet maps. I want to plot the trajectories, like the points and the lines connection those points on the map (you can suggest any map, not just leaflets, I want a real map) and in second visualization I want to show a simple line plot with x, y coordinates, no time. I am really sorry for the unclear question. – Liza Mar 20 '17 at 18:55
  • @roganjosh I am sorry, for the confusions. I am working in python, and I guess we can use Folium to work on leaflet maps. I want to plot the trajectories, like the points and the lines connection those points on the map (you can suggest any map, not just leaflets, I want a real map) and in second visualization I want to show a simple line plot with x, y coordinates, no time. I am really sorry for the unclear question. – Liza Mar 20 '17 at 18:58
  • So the output map should be html in a browser? – ImportanceOfBeingErnest Mar 20 '17 at 19:04
  • @ImportanceOfBeingErnest Yes, is it possible ? – Liza Mar 20 '17 at 19:07
  • Ok, that means that you don't want to use matplotlib at all. For sure it's possible but I would suggest to work a bit on your own and find out where the problem really is, as providing a generic solution is really too broad for the Stackoverflow Q&A style. – ImportanceOfBeingErnest Mar 20 '17 at 19:13
  • Since this is trajectories, do you have any insight into whether [https://matplotlib.org/basemap/](https://matplotlib.org/basemap/) can cover this btw @ImportanceOfBeingErnest? – roganjosh Mar 20 '17 at 19:21
  • Yes basemap can plot a map with trajectories, but the output is an interactive window or a static image file, you cannot have interactive html output. – ImportanceOfBeingErnest Mar 20 '17 at 19:25
  • Thanks @ImportanceOfBeingErnest . I stand by my initial comments then; OP is going to need JavaScript and this question is too broad, in agreement with you. I've just seen some remarkable things come out of `basemap` that don't seem possible from the introductory docs, so worth a quick check :) – roganjosh Mar 20 '17 at 19:28
  • @roganjosh yes basemap can plot trajectories, but it's not interactive html output. – Liza Mar 20 '17 at 19:29

1 Answers1

0

You could convert your dataframe to JSON format which is easily handled by any Javascript mapping library.

For instance using Pandas DataFrame.to_json method turns this dataframe:

voyage  id  x           y           time
27      2   -7.35857534 2.09175178  1.29471228
28      2   -7.35863779 2.09167080  1.29471234
29      2   -7.35882203 2.09156224  1.29471240

Into this JSON array:

[{
    "voyage": 27,
    "id": 2,
    "x": -7.35857534,
    "y": 2.09175178,
    "time": 1.29471228
}, {
    "voyage": 28,
    "id": 2,
    "x": -7.35863779,
    "y": 2.09167080,
    "time": 1.29471234
}, {
    "voyage": 29,
    "id": 2,
    "x": -7.35882203,
    "y": 2.09156224,
    "time": 1.29471240
}]

The resulting file can be loaded by Javascript and used with your mapping library like Leaflet:

// Start a new map
var map = new L.Map('leaflet', {'center': [0, 0], 'zoom': 0});

// Load file 
fetch('data.json').then(function(response) {

    // Return JSON 
    return response.json().then(function (arr) {

        // Add a line
        var line = new L.Polyline([]).addTo(map);

        // Iterate the objects in the array
        arr.forEach(function (obj) {

            // If ID is not 0
            if (obj.id) {

                // Add point to line
                line.addLatLng(obj.y, obj.x);

            // ID is 0
            } else {

                // Start new line
                line = new L.Polyline([]).addTo(map);
            }
        });
    });
});
iH8
  • 27,722
  • 4
  • 67
  • 76