# Using boost-histogram

```none
[1]:
```

## 1: Basic 1D histogram

Let’s start with the basics. We will create a histogram using boost-histogram and fill it.

### 1.1: Data

Let’s make a 1d dataset to run on.

```none
[2]:
```

Now, let’s make a histogram

```none
[3]:
```

```none
[4]:
```

```none
[4]:
```

You can see that the histogram has been filled. Let’s explicitly check to see how many entries are in the histogram:

```none
[5]:
```

```none
[5]:
```

What happened to the missing items? They are in the underflow and overflow bins:

```none
[6]:
```

```none
[6]:
```

Like ROOT, we have overflow bins by default. We can turn them off, but they enable some powerful things like projections.

Let’s plot this (Hist should make this easier):

```none
[7]:
```

Note: you can select the axes before or after calling `.centers`; this is very useful for ND histograms.

From now on, let’s be lazy

```none
[8]:
```

Aside: here’s step. The edges are quite ugly for us, just like it is for numpy. Or anyone.

```none
[9]:
```

Recent versions of matplotlib support `.stairs`, which was designed to work well with histograms:

```none
[10]:
```

No plotting is built in, but the data is easy to access.

## 2: Drop-in replacement for NumPy

To start using this yourself, you don’t even need to change your code. Let’s try the numpy adapters.

```none
[11]:
```

```none
[12]:
```

```none
[13]:
```

```none
[13]:
```

```none
[14]:
```

```none
[14]:
```

Not bad! Let’s start moving to the boost-histogram API, so we can use our little plotting function:

```none
[15]:
```

Now we can move over to boost-histogram one step at a time! Just to be complete, we can also go back to a NumPy tuple from a Histogram object:

```none
[16]:
```

```none
[16]:
```

## 3: More dimensions

The same API works for multiple dimensions.

```none
[17]:
```

```none
[18]:
```

```none
[19]:
```

From here on out, I will be using `.reset()` before a `.fill()`, just to make sure each cell in the notebook can be rerun.

```none
[20]:
```

```none
[20]:
```

Again, let’s make plotting a little function:

```none
[21]:
```

This is transposed because `pcolormesh` [expects it](https://matplotlib.org/3.2.2/api/_as_gen/matplotlib.pyplot.pcolormesh.html#axes-pcolormesh-grid-orientation).

```none
[22]:
```

Let’s try a 3D histogram

```none
[23]:
```

```none
[23]:
```

Let’s project to the first two axes:

```none
[24]:
```

## 4: UHI

Let’s explore the boost-histogram UHI syntax. We will reuse the previous 2D histogram from part 3:

```none
[25]:
```

I can see that I want y from 0.25 to 0.75, in data coordinates:

```none
[26]:
```

What’s the contents of a bin?

```none
[27]:
```

```none
[27]:
```

How about in data coordinates?

```none
[28]:
```

```none
[28]:
```

> Note: to get the coordinates manually:

> ```python
> hist3.axes[0].index(.5) == 100
> hist3.axes[1].index(.75) == 87
> ```

How about a 1d histogram?

```none
[29]:
```

Let’s look at one part and rebin:

```none
[30]:
```

What is the value at `(-.75, .5)`?

```none
[31]:
```

```none
[31]:
```

## 5: Understanding accumulators

Boost-histogram has several different storages; storages store accumulators. Let’s try making a profile.

```none
[32]:
```

```none
[32]:
```

Here’s a quick example accessing the values:

```none
[33]:
```

## 6: Changing the storage

```none
[34]:
```

```none
[35]:
```

```none
[35]:
```

```none
[36]:
```

```none
[36]:
```

```none
[37]:
```

```none
[37]:
```

```none
[38]:
```

```none
[38]:
```

```none
[39]:
```

```none
[39]:
```

## 7: Making a density histogram

Let’s try to make a density histogram like NumPy’s.

```none
[40]:
```

Yes, it’s ugly. Don’t judge.

We don’t have a `.density`! What do we do? (note: `density=True` is supported if you do not return a bh object)

```none
[41]:
```

```none
[41]:
```

Yes, that does not need to be so complicated for 1D, but it’s general.

```none
[42]:
```

```none
[43]:
```

## 8: Axis types

There are more axes types, and they all provide the same API in histograms, so they all just work without changes:

```none
[44]:
```

```none
[45]:
```

```none
[45]:
```

```none
[46]:
```

## 9: And, circular, too!

```none
[47]:
```

```none
[47]:
```

Now, the really complicated part, making a circular histogram:

```none
[48]:
```
