Matplotlib Visualization with XKCD Style

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Matplotlib is a data visualization library that provides various tools to create 2D and 3D plots. It is built on top of the NumPy library and its main purpose is to visualize data in a simple and effective way. In this tutorial, we will learn how to create two different types of plots using the xkcd style from the XKCD webcomic.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Importing Libraries

The first step is to import the necessary libraries. We will be using the matplotlib.pyplot and numpy libraries.

import matplotlib.pyplot as plt
import numpy as np

Creating an XKCD-style Plot

In this step, we will create an xkcd-style plot that shows the relationship between time and overall health. The plot is based on the "Stove Ownership" comic from XKCD.

with plt.xkcd():
    fig = plt.figure()
    ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))
    ax.spines[['top', 'right']].set_visible(False)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_ylim([-30, 10])

    data = np.ones(100)
    data[70:] -= np.arange(30)

    ax.annotate(
        'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED',
        xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10))

    ax.plot(data)

    ax.set_xlabel('time')
    ax.set_ylabel('my overall health')
    fig.text(
        0.5, 0.05,
        '"Stove Ownership" from xkcd by Randall Munroe',
        ha='center')

plt.show()

Creating a Bar Chart with XKCD Style

In this step, we will create a bar chart with the XKCD style. The chart is based on the "The Data So Far" comic from XKCD.

with plt.xkcd():
    fig = plt.figure()
    ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))
    ax.bar([0, 1], [0, 100], 0.25)
    ax.spines[['top', 'right']].set_visible(False)
    ax.xaxis.set_ticks_position('bottom')
    ax.set_xticks([0, 1])
    ax.set_xticklabels(['CONFIRMED BY\nEXPERIMENT', 'REFUTED BY\nEXPERIMENT'])
    ax.set_xlim([-0.5, 1.5])
    ax.set_yticks([])
    ax.set_ylim([0, 110])

    ax.set_title("CLAIMS OF SUPERNATURAL POWERS")

    fig.text(
        0.5, 0.05,
        '"The Data So Far" from xkcd by Randall Munroe',
        ha='center')

plt.show()

Summary

In this tutorial, we learned how to create two different types of plots using the xkcd style from the XKCD webcomic. We first created an xkcd-style plot that shows the relationship between time and overall health. We then created a bar chart with the XKCD style that is based on the "The Data So Far" comic from XKCD. By using the xkcd style, we can create fun and engaging visualizations that are sure to capture the attention of our audience.

Other Python Tutorials you may like