question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding cyclic point feature

See original GitHub issue

I will address the same issue as for Cartopy (https://github.com/SciTools/cartopy/issues/1402). I would find extremely useful to have an automatic behavior of adding a cyclic point when needed, or at least add an option when doing a plot.

Here is an example of what I mean (data: ens_avg_snow.zip):

import xarray as xr
import proplot as plot

ens_avg_snow = xr.open_dataarray('ens_avg_snow.nc')

f, axs = plot.subplots(proj='cyl', width=8)
m = axs[0].contourf(ens_avg_snow, cmap='BuRd')
f.colorbar(m)
axs.format(
    geogridlinewidth=0.5, geogridcolor='gray8', geogridalpha=0.5, labels=True, 
    coast=True, ocean=True, oceancolor='gray3', latlim=(20,90),
)

proplot_cyclic_point

As you can see, there is a white line at 0° of longitude. I usually use the add_cyclic_point function of cartopy.utils but it is not straightforward for DataArrays because it doesn’t keep the coordinates and attributes. I recently found some very useful code here: https://github.com/darothen/plot-all-in-ncfile/blob/master/plot_util.py that allows to do it easily with the function cyclic_dataarray and there is also a check_cyclic that could be used for automatically checking it.

Here is the function that allows adding a cyclic point (from the link above):

from cartopy.util import add_cyclic_point

# https://github.com/darothen/plot-all-in-ncfile/blob/master/plot_util.py
def cyclic_dataarray(da, coord='lon'):
    """ Add a cyclic coordinate point to a DataArray along a specified
    named coordinate dimension.
    """
    assert isinstance(da, xr.DataArray)

    lon_idx = da.dims.index(coord)
    cyclic_data, cyclic_coord = add_cyclic_point(da.values,
                                                 coord=da.coords[coord],
                                                 axis=lon_idx)

    # Copy and add the cyclic coordinate and data
    new_coords = dict(da.coords)
    new_coords[coord] = cyclic_coord
    new_values = cyclic_data

    new_da = xr.DataArray(new_values, dims=da.dims, coords=new_coords)

    # Copy the attributes for the re-constructed data and coords
    for att, val in da.attrs.items():
        new_da.attrs[att] = val
    for c in da.coords:
        for att in da.coords[c].attrs:
            new_da.coords[c].attrs[att] = da.coords[c].attrs[att]

    return new_da

Thus ens_avg_snow_cyclic = cyclic_dataarray(ens_avg_snow) allows to make back the previous plot without the white line. Incorporating this directly into ProPlot would be nice so that we can just add an option like for example add_cyclic=True (just need to be careful about the dimension to do the cyclic or have another way to pass it).

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lukelbdcommented, Dec 4, 2019

Yep globe=True is the way to do this.

This is implemented by the standardize_2d wrapper. When #45 is implemented this will be documented on the individual ProjAxes.contourf, ProjAxes.pcolor, etc. methods.

1reaction
bradyrxcommented, Dec 4, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

cartopy.util.add_cyclic_point - SciTools
Add a cyclic point to an array and optionally a corresponding coordinate. ... Adding a cyclic point to a data array, where the...
Read more >
Example of adding cyclic points to DataArrays for plotting with ...
Illustrating how to add a wrap-around longitude to a Dataset in xarray. First, we can open the dataset as normal, and plot using...
Read more >
Example Map Plotting - Python Resources for CAM-chem
... contour plot of variable as world map with coastlines # - Add cyclic point # - Customize contours and colorbar # -...
Read more >
Xarray Roll and Cartopy add_cyclic_point functions conflicting
') ValueError: The coordinate must be equally spaced. As it says, I can't add the point to the array because it is not...
Read more >
Time-related feature engineering - Scikit-learn
This also has the added benefit of preventing any issue with unknown categories when using cross-validation. The numerical variables need no preprocessing and, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found