home / github / issues

Menu
  • Search all tables
  • GraphQL API

issues: 159791786

This data as json

id node_id number title user state locked assignee milestone comments created_at updated_at closed_at author_association active_lock_reason draft pull_request body reactions performed_via_github_app state_reason repo type
159791786 MDU6SXNzdWUxNTk3OTE3ODY= 880 Possibility to provide default map background/ elements to plot 13906519 closed 0     2 2016-06-11T20:11:25Z 2019-02-26T17:51:16Z 2019-02-26T17:51:16Z NONE      

Hi.

Really enjoying xarray! Converting all my scripts and on the quest to simplify my plotting.

For my current project I need loads of maps and generally add a land/ ocean layer and country boundaries as well as an default extent to the plot axes...

I use for instance a extra layer like this:

``` import cartopy.crs as cars import cartopy.feature as feature

default map backgrounds

countries = cfeature.NaturalEarthFeature( category='cultural', name='admin_0_countries', scale='50m', facecolor='none')

```

An example from a class I work on right now:

```

def plot_monthly(self, figsize=(12,10)):
    _unit = 'n.d.'
    _name = 'var' 
    if 'units' in self._dataArray.attrs.keys():
        _unit = self._dataArray.attrs['units']


    self._fig, self._axes = plt.subplots(ncols=4, nrows=3, \
                                         figsize=figsize, subplot_kw={'projection': ccrs.PlateCarree()})

    _min = self._dataArray.min()
    _max = self._dataArray.max()

    print self._dataArray

    for i, ax in enumerate(self._axes.flatten()):
        ax.set_extent( self._extent )
        ax.add_feature(self._countries, edgecolor='gray')     
        ax.add_feature(self._ocean)                           
        ax.coastlines(resolution='50m')
        ax.set_title(self._monthNames[i+1])
        _plot = xr.plot.plot(self._dataArray.sel(month=i+1), ax=ax, \
                        vmin=_min, vmax=_max, \
                        transform=ccrs.PlateCarree(), \
                        add_labels=False,
                        robust=True, add_colorbar=False)


    self._fig.subplots_adjust(right=0.8)
    _cbar_ax = self._fig.add_axes([0.85, 0.15, 0.05, 0.7])

    _var_str = "%s [%s]" % (_name, _unit)
    self._fig.colorbar(_plot, cax=_cbar_ax, label=_var_str)

```

This is a bit clumsy at the moment - I basically define the axes for each subplot (say 1..12 month), loop over them, select the axis and add them with ax.set_extent(), ax.add_feature() etc...

This works, but I'd rather use the plot function within DataArray or DataSet... Also thinking about using FacetGrid instead of doing it manually.

I thought about using the new decorators to patch this onto a custom plot directive but my Python is not really advanced enough for decorators and the inner functioning of array it seems...

``` @xr.register_dataarray_accessor('map') class MapDecorator(object): def init(self, xarray_obj): self._obj = xarray_obj

def plot(self, **kwargs):
    """Plot data on a map."""

    print "my decorator"

    #ax.set_extent([102, 110, 8, 24])
    ## add map elements
    #ax.add_feature(countries, edgecolor='gray')     # country borders
    #ax.add_feature(ocean)                           # ocean
    #ax.coastlines(resolution='50m')                 # coastlines

    p = self._obj.plot( kwargs )
    return p

```

Not sure if this is a valid path or how one would do that? I would also like to have some default arguments (projection=)

Cheers, Christian

{
    "url": "https://api.github.com/repos/pydata/xarray/issues/880/reactions",
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  completed 13221727 issue

Links from other tables

  • 1 row from issues_id in issues_labels
  • 2 rows from issue in issue_comments
Powered by Datasette · Queries took 0.574ms · About: xarray-datasette