logo

... matplot gif.


An image can say a thousand words. But sometimes having a gif can make it just a bit easier. In this series of video's we'll highlight a tool called gif that makes it easy to turn matplotlib plots into gifs.


Episode Notes

The final example demonstrates how you can use a gif to explain what a hyperparameter does in a machine learning model.

import gif
import numpy as np
import matplotlib.pylab as plt
from IPython.display import Image
from sklearn.ensemble import RandomForestRegressor

@gif.frame
def frame(i):
    mod = RandomForestRegressor(n_estimators=1, max_depth=i)
    mod.fit(x.reshape(-1, 1), y)
    plt.plot(x, y)
    plt.plot(x, mod.predict(x.reshape(-1, 1)))
    plt.title(f"max_depth={i}")

frames = [frame(i) for i in range(100)]
gif.save(frames, "rf.gif", duration=300)
Image("rf.gif")

A thing to remember: gifs can help explain something but it should never replace a simple line chart. Don't over-use them.


Feedback? See an issue? Feel free to mention it here.

If you want to be kept up to date, consider getting the newsletter.