Skip to content

[Feature request]: xarray accessor #405

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.

importxarrayasxrairtemps=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:

importproplotaspplt@xr.register_dataarray_accessor("proplot")classProPlotAccessor:
def__init__(self, xarray_obj):
self=xarray_objdefplot1D(self):
fig,ax=pplt.subplots(nrows=1,ncols=1)
ax.plot(self,color='k')
defplot2D(self):
fig,ax=pplt.subplots(nrows=1,ncols=1)
ax.contourf(self,cmap='viridis')
defplot(self):
iflen(self.dims) ==1: # if data is 1Dreturnself.plot1D()
eliflen(self.dims) ==2: # if data is 2Dreturnself.plot2D()
else:
raiseValueError("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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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