Skip to content

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

Description

@jerabaul29

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:

  • the "ugly line" in itself: this could be removed so that the trajectory plot only goes from the left side, to the "left end", and then to the "right end" to the right side, without the "ugly line".

  • in cases when there is only information around the -180/+180 area, and no information / lines e.g. around longitude 0, the projection for the plot could be centered around the "center of mass" of the trajectories, rather than around longitude 0. Note that this would only solve the problem if having for example only data between [175; wrapped 185], but this would not help if there are "global" trajectories running globally around Earth.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions