Implementing Comic Visualization Python

Comic Visualization FeaImg

We all know that the average individual responds far better to visual information than to text alone. Typically, the graphs generated by Matplotlib are quite flawless but boring. Observing these graphs is not really entertaining.

In this tutorial, we will strive to make conventional images more entertaining and humorous, using XKCD as an example.

The webcomic xkcd was established by the American author Randall Munroe in 2005. xkcd is a weekly internet comic strip that is updated weekly. It is among the most popular comic books. We will here try to paint our plots in the style of xkcd comics!


Load Libraries and Data

We will begin by importing the necessary libraries and then proceed to import the data into the program. Here, we load the tips dataset and output its initial five rows.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from seaborn import load_dataset

tips = load_dataset("tips")
tips.head()
Tips Dataset
Tips Dataset

Example 1 – Bar Plots

A bar graph is a graphical representation of data in which categories can be highlighted with specific forms, such as a rectangle. The length and height of the bars in the bar chart show the distribution of data within the dataset.

Also Read: Python Bar Plot – Visualize Categorical Data in Python

Using the sns.countplot function, which plots the counts of unique values in a column, we can generate a basic bar chart.

plt.figure(facecolor="W",figsize=(10,5))
plt.title("Bar Plot - Normal")
sns.countplot(tips['sex'])
plt.show()
BarPlot Normal
BarPlot Normal

To make the plot comic, we need to add the plt.xkcd method before the entire code.

BarPlot Comic
BarPlot Comic

Example 2: Box Plot

Boxplots are a measurement of the data set’s data distribution. It separates the data set into quartiles. This graph depicts the data set’s minimum, maximum, median, first quartile, and third quartile.

Also Read: Boxplots: Everything you need to know

Using the code below, we can generate a boxplot using the sns.boxplot function.

plt.figure(facecolor="W",figsize=(10,5))
plt.title("BoxPlot - Normal")
sns.boxplot(x = "day", y = "total_bill", hue = "sex", data = tips);
plt.show()
BoxPlot Normal
BoxPlot Normal

To make a plot comic, we need to add the plt.xkcd method before the entire code.

plt.xkcd()
plt.figure(facecolor="W",figsize=(10,5))
plt.title("BoxPlot - Comic")
sns.boxplot(x = "day", y = "total_bill", hue = "sex", data = tips);
plt.show()
BoxPlot Comic
BoxPlot Comic

Example 3 – Sine Wave

In this example, we will generate the values for a sine wave plot by utilizing the linspace and sin functions, as seen in the code below.


plt.figure(facecolor="W",figsize=(10,5))
plt.plot(np.sin(np.linspace(0, 10)))
plt.title('A simple Sine Wave - Normal')
plt.show()
Sine Wave Normal
Sine Wave Normal

To make a plot comic, we need to add the plt.xkcd method before the entire code.

plt.xkcd()
plt.figure(facecolor="W",figsize=(10,5))
plt.plot(np.sin(np.linspace(0, 10)))
plt.title('A simple Sine Wave - Comic')
plt.show()
Sine Wave Comic
Sine Wave Comic

Example 4 – Pie Chart

Matplotlib’s pie() function supports pie charts. The plt.pie() method can be used to generate a plot. The following code generates a pie chart:

labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 210]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0, 0, 0)
plt.figure(facecolor="W",figsize=(10,5))
plt.pie(sizes, explode=explode, labels=labels, colors=colors,autopct='%1.1f%%')
plt.axis('equal')
plt.title("Pie Chart - Normal")
plt.show()
PieChart Normal
PieChart Normal

To make plot comic, we need to add the plt.xkcd method before the entire code.

labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 210]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0, 0, 0)
plt.xkcd()
plt.figure(facecolor="W",figsize=(10,5))
plt.pie(sizes, explode=explode, labels=labels, colors=colors,autopct='%1.1f%%')
plt.axis('equal')
plt.title("Pie Chart - Comic")
plt.show()
PieChart Comic
PieChart Comic

Conclusion

I hope you have enjoyed the tutorial from the boring standard visualizations to fun and interesting comic ones with the help of xkcd. I would also recommend you the following tutorials:

  1. Data Visualization using Python Bokeh
  2. Animating Data in Python – A Simple Guide
  3. ASCII Art in Python Programming Language

Thank you for reading! Happy Learning! 😃