Skip to content

[Feature request]: xarray accessor #405

Open
@savannahferretti

Description

@savannahferretti

I am a PhD student in climate science, and love proplot! But I would love to use it from xarray using an accessor. That might look like this:

So you can toy with the data yourself, I used the Xarray tutorial air temperature dataset for an "example" of sorts. The environment needs both pooch (```conda install pooch``) and Xarray installed.

import xarray as xr

airtemps = xr.tutorial.open_dataset("air_temperature")
airtemp1D = airtemps.mean(dim=['lat','lon']).air # example of 1D (time)
airtemp2D = airtemps.isel(time=0).air # example of 2D (lat,lon)
airtemps3D = airtemps.air # example of 3D (time,lat,lon)

Make plotting simple graphs in proplot an accessor to Xarray:

import proplot as pplt

@xr.register_dataarray_accessor("proplot")
class ProPlotAccessor:
    def __init__(self, xarray_obj):
        self = xarray_obj

    def plot1D(self):
        fig,ax = pplt.subplots(nrows=1,ncols=1)
        ax.plot(self,color='k')
        
    def plot2D(self):
        fig,ax = pplt.subplots(nrows=1,ncols=1)
        ax.contourf(self,cmap='viridis')
        
    def plot(self):
        if len(self.dims) == 1:  # if data is 1D
            return self.plot1D()
        elif len(self.dims) == 2:  # if data is 2D
            return self.plot2D()
        else:
            raise ValueError("Data is not 1D or 2D")

Test the accessor on the Xarray tutorial data with different dimensions:

airtemps1D.proplot.plot()

image

airtemps2D.proplot.plot()

image

I think this would be impactful because it would make it easier for those in the climate science space who use Xarray to get their feet wet with proplot, moving away from matplotlib. I'd be happy to collaborate on this effort if wanted/needed!

@TomNicholas

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions