# Create a Relative Frequency Histogram in Matplotlib

A is a graph that displays the relative frequencies of values in a dataset.

You can use the following syntax to create a relative frequency histogram in Matplotlib in Python:

```import matplotlib.pyplot as plt
import numpy as np

#define plotting area
fig = plt.figure()

#create relative frequency histogram
ax.hist(data, edgecolor='black', weights=np.ones_like(data) / len(data))
```

The following example shows how to use this syntax in practice.

### Example: Create Relative Frequency Histogram in Matplotlib

The following code shows how to create a regular frequency histogram in Matplotlib:

```import numpy as np
import matplotlib.pyplot as plt

#define data values
data = [8, 8, 9, 12, 13, 13, 14, 14, 15, 18, 22, 23, 24, 25, 30]

#create frequency histogram
fig = plt.figure()
ax.hist(data, edgecolor='black')```

The x-axis shows the bins for the data values and the y-axis shows the frequency for each bin.

We can use the following code to instead display relative frequencies on the y-axis:

```import numpy as np
import matplotlib.pyplot as plt

#define data values
data = [8, 8, 9, 12, 13, 13, 14, 14, 15, 18, 22, 23, 24, 25, 30]

#create relative frequency histogram
fig = plt.figure()
ax.hist(data, edgecolor='black', weights=np.ones_like(data) / len(data))```

The y-axis now displays relative frequencies.

For example, there are 15 total values in the dataset.

So instead of showing a frequency of 4 on the y-axis for the tallest bar in the graph, the y-axis now shows 4/15 = 0.2667.

We can also use the PercentFormatter() function from Matplotlib to display the values on the y-axis as percentages:

```import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter

#define data values
data = [8, 8, 9, 12, 13, 13, 14, 14, 15, 18, 22, 23, 24, 25, 30]

#create relative frequency histogram with percentages on y-axis
fig = plt.figure()