# XArray Example

To run this example, an environment.yml similar to this one could be used:

```yaml
name: bh_xhistogram
channels:
  - conda-forge
dependencies:
  - python==3.9
  - boost-histogram
  - xhistogram
  - matplotlib
  - netcdf4
```

```none
[1]:
```

Let’s look at using boost-histogram to imitate the xhistogram package by reading and producing xarrays. As a reminder, xarray is a sort of generalized Pandas library, supporting ND labeled and indexed data.

## Simple 1D example

We will start with the first example from the xhistogram docs:

```none
[2]:
```

### xhistogram

And, this is what historamming and plotting looks like:

```none
[3]:
```

### boost-histogram (direct usage)

Let’s first just try this by hand, to see how it works. This will not return an xarray, etc.

```none
[4]:
```

### boost-histogram (adapter function)

Now, let’s make an adaptor for boost-histogram.

```none
[5]:
```

```none
[6]:
```

#### More features

Let’s add a few more features to our function defined above.

- Let’s allow bins to be a list of axes or even a completely prepared histogram; this will allow us to take advantage of boost-histogram features later.
- Let’s add a weights keyword so we can do weighted histograms as well.

```none
[7]:
```

## 2D example

This mirrors the xhistogram docs, but uses a small synthetic ocean-like dataset so the notebook stays self-contained and reliable in CI.

```none
[8]:
```

```none
[9]:
```

### xhistogram

```none
[10]:
```

```none
[10]:
```

### boost-histogram

We could hand in the same bin definitions, but let’s use boost-histogram axes instead:

```none
[11]:
```

```none
[11]:
```

#### Speed comparson

```none
[12]:
```

```none
[13]:
```

#### Weighted histogram

Let’s try a more complex example from the docs; the dVol weights one:

```none
[14]:
```

```none
[14]:
```
