If you’d like to smooth out your jagged jagged lines in pandas, you’ll want compute a rolling average. So instead of the original values, you’ll have the average of 5 days (or hours, or years, or weeks, or months, or whatever).

Using .rolling in pandas to compute a rolling mean or median

import pandas as pd
import matplotlib.pyplot as plt

%matplotlib inline
df = pd.read_csv("../pr-deaths.csv")
df.head()
SEP Y2015 Y2016 Y2017
0 1 75 75 92
1 2 77 67 69
2 3 67 78 78
3 4 71 99 84
4 5 62 89 75
df.plot(x='SEP', ylim=(0, 150))
<matplotlib.axes._subplots.AxesSubplot at 0x1117def60>

png

You can use df.rolling, and then ask it for the .mean(). The code we’re going to use is

df.rolling(3, on='SEP').mean()

Let’s break it down:

* `5` means, we want to combine 5 values
* `on=` means **pay attention to the order of the `SEP` column**
* `.mean()` means we want to take the mean of those 5 values (you could also use `.median()` or `.max()` or anything else)

So it will take every 5 values and take their mean. You’ll see NaN for the first 4 days because we’re requiring 5 values before we take the mean.

Now let’s see it in action:

df.rolling(5, on='SEP').mean()
SEP Y2015 Y2016 Y2017
0 1 NaN NaN NaN
1 2 NaN NaN NaN
2 3 NaN NaN NaN
3 4 NaN NaN NaN
4 5 70.4 81.6 79.6
5 6 70.8 81.4 77.2
6 7 72.4 81.4 80.8
7 8 75.8 81.2 83.8
8 9 77.4 79.4 81.4
9 10 78.2 76.2 86.0
10 11 81.2 77.0 88.4
11 12 80.0 76.8 86.4
12 13 79.4 79.0 87.8
13 14 77.6 77.2 89.4
14 15 81.8 80.8 86.4
15 16 77.4 79.4 83.4
16 17 75.6 79.8 85.0
17 18 74.6 78.0 80.4
18 19 76.8 78.2 79.2
19 20 73.2 75.8 82.6
20 21 73.2 75.0 91.2
21 22 72.8 80.8 96.2
22 23 71.6 79.2 102.6
23 24 71.0 78.6 111.8
24 25 69.2 77.8 118.8
25 26 70.4 80.8 121.0
26 27 72.4 77.8 123.0
27 28 75.2 79.8 123.0
28 29 76.2 78.0 124.4
29 30 78.8 81.2 123.2

Great! Maybe it’s reasonable? Let’s see how it looks graphed.

df.rolling(5, on='SEP').mean().plot(x='SEP', ylim=(0, 150))
<matplotlib.axes._subplots.AxesSubplot at 0x115a43898>

png

Using plt.subplots to plot two graphs next to each other

Remember how we sometimes do fig, ax = plt.subplots() to create an empty graph? We can also use it (with some more options) to create two empty graphs. They end up in the same image, and it allows us to plot them on top of or next to each other.

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(15, 5))

ax1.set_title("Original data")
df.plot(x='SEP', ylim=(0, 150), ax=ax1)

ax2.set_title("Rolling 5-day mean")
df.rolling(5, on='SEP').mean().plot(x='SEP', ylim=(0, 150), ax=ax2)
<matplotlib.axes._subplots.AxesSubplot at 0x11418f4e0>

png