OpenDrift / trajan

Trajectory analysis package for simulated and observed trajectories
https://opendrift.github.io/trajan/
GNU General Public License v2.0
11 stars 5 forks source link

plots crossing the "-180/+180" wrapping longitude line #126

Open jerabaul29 opened 1 month ago

jerabaul29 commented 1 month ago

Plots crossing the "-180/+180" wrapping longitude line have "ugly" lines appearing on them. Consider for example the dummy trajectory:

# %%

import matplotlib.pyplot as plt
import numpy as np
import xarray as xr

import cartopy.crs as ccrs

import trajan as _

# %%

# generate the xarray in trajan format
xr_data = xr.Dataset(
    {
        # meta vars
        'trajectory':
        xr.DataArray(data=np.array(["test"]),
                     dims=['trajectory'],
                     attrs={
                         "cf_role": "trajectory_id",
                         "standard_name": "platform_id",
                         "units": "1",
                         "long_name": "ID / name of each buoy present in the deployment data.",
                     }).astype(str),

        # trajectory vars
        'time':
            xr.DataArray(dims=["trajectory", "obs"],
                         data=np.array([[np.datetime64('2024-10-17T16:00:00'), np.datetime64('2024-10-17T17:00:00'), np.datetime64('2024-10-17T18:00:00'), np.datetime64('2024-10-17T19:00:00')],]),
                         attrs={
                             "standard_name": "time",
                             "long_name": "Time for the GNSS position records.",
            }),
            #
            'lat':
            xr.DataArray(dims=["trajectory", "obs"],
                         data=np.array([[0.0, -10.0, -11, -20],]),
                         attrs={
                             "_FillValue": "NaN",
                             "standard_name": "latitude",
                             "units": "degree_north",
            }),
            #
            'lon':
            xr.DataArray(dims=["trajectory", "obs"],
                         data=360.0 * 0.0 + np.array([[-159.5, -179.5, 179.5, 159.5],]),
                         attrs={
                             "_FillValue": "NaN",
                             "standard_name": "longitude",
                             "units": "degree_east",
            }),
    }
)

# %%

plt.figure()

xr_data.traj.plot(color="red")

plt.show()

# %%

This generates a plot that looks like:

Screenshot from 2024-10-17 15-07-37

Would this make sense to improve on in trajan?

I think that there are several aspects to consider:

I am looking a bit into this, I can try to populate this issue as I dig in fixes in cartopy and similar.

jerabaul29 commented 1 month ago

thought 1: simple workaround to avoid the ugly line: use linestyle="none", marker="."

A simple workaround to avoid the ugly line is to use linestyle="none", marker=".", markersize=10 or similar. For example:

# %%

plt.figure()

proj = ccrs.PlateCarree(central_longitude=179.0)

ax = plt.axes(projection=proj)
xr_data.traj.plot(linestyle="none", marker=".", markersize=10, color="red")
ax.coastlines()

lon1, lon2, lat1, lat2 = 155, 210, -25.0, 5.0
ax.set_extent([lon1, lon2, lat1, lat2], crs=ccrs.PlateCarree())

plt.show()

# %%

generates something that looks like:

Screenshot from 2024-10-17 17-18-57

A few notes:

lon1, lon2, lat1, lat2 = 155, 210, -25.0, 5.0
ax.set_extent([lon1, lon2, lat1, lat2], crs=ccrs.PlateCarree())

part, then the plot looks like

Screenshot from 2024-10-17 17-22-02

jerabaul29 commented 1 month ago

I see a few things that could be considered here; what are your thoughts / what would you like me to give a try at implementing @gauteh ? :)

gauteh commented 1 month ago

Just a quick comment now, we already have scatter: https://github.com/OpenDrift/trajan/blob/73260cc1a1b737a140dae02d7c52d5713e341851/trajan/plot/__init__.py#L201 . ds.traj.plot() is short for ds.traj.plot.lines().

jerabaul29 commented 1 month ago

@gauteh thank you for the tips about scatter! :)

By running

xr_data.traj.plot.scatter()
plt.show()

I get this plotted, fast:

Screenshot from 2024-10-18 12-25-48

So this is very nice and nearly fits my need :) . I think the "only" issue is that now all trajectories are in teh same color, but I am not sure if there is an easy way around with the scatter function.

gauteh commented 1 month ago

trajan uses a lower alpha if there are "many" trajectories, but in theory it should be possible to do that exactly as with the lines function. I don't think it is implemented yet though: https://github.com/OpenDrift/trajan/blob/main/trajan/plot/__init__.py#L172