XKCD-style Plot using matplotlib

Goal

This post aims to introduce how to plot the data using matplotlib in an XKCD style.

image

Libraries

In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

Create data to plot

In [2]:
x = np.linspace(-np.pi, np.pi, 100)
df_data = pd.DataFrame(data={'sin x': np.sin(x), 'cos x': np.cos(x)}, index=x)
df_data.head()
Out[2]:
sin x cos x
-3.141593 -1.224647e-16 -1.000000
-3.078126 -6.342392e-02 -0.997987
-3.014660 -1.265925e-01 -0.991955
-2.951193 -1.892512e-01 -0.981929
-2.887727 -2.511480e-01 -0.967949
In [3]:
df_data.plot(title='Normal Matplotlib Style');
In [4]:
with plt.xkcd():
    df_data.plot(title='XKCD Style');

Comments

Comments powered by Disqus