Matplotlib - XKCD Style



XKCD style refers to a particular type of comic art and humour inspired by the webcomic "XKCD", created by Randall Munroe. XKCD comics are generally known for their modest drawings, often stick-figure characters, and simple black-and-white artwork. The comic covers a wide range of topics, including science, mathematics, technology, and everyday life, presenting them in a humorous and thought-provoking manner.

XKCD Style in Matplotlib

In Matplotlib, XKCD style refers to a feature that allows you to make plots look like they are hand-drawn with a marker, similar to the style of the XKCD webcomic by Randall Munroe. This means the lines in your plots will appear wobbly and not perfectly straight, giving them a playful and informal look. It is a fun way to visualize your data with a touch of humour or character to your plots.

To draw plots in XKCD style in Matplotlib, you can use the plt.xkcd() function. This function enables the XKCD mode, which transforms subsequent plots to mimic the hand-drawn aesthetic characteristic of XKCD comics.

Basic XKCD Style Plot

A basic XKCD style plot in Matplotlib is a type of graph that adopts the whimsical and hand-drawn appearance inspired by the XKCD webcomic. It gives your plots a playful and informal look with irregular lines, handwritten fonts, and exaggerated features, similar to the style used in XKCD comics.

Example

In the following example, we are creating a simple line plot using XKCD style. The plot shows the relationship between X and Y values −

import matplotlib.pyplot as plt
# Enabling the XKCD mode
plt.xkcd()
# Creating a simple XKCD style plot
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'b-')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('XKCD Style Plot')
plt.show()

Output

Following is the output of the above code −

Basic XKCD Style Plot

XKCD Style Scatter Plot

In Matplotlib, the XKCD style scatter plot provides a quirky and hand-drawn look to your data visualization. By enabling XKCD mode with plt.xkcd(), your scatter plot takes on a whimsical appearance, with points resembling doodles drawn by hand. This style adds a fun and informal touch to your scatter plots, making them more engaging and visually interesting.

Example

In here, we are using the XKCD mode to create a scatter plot with "red" points. Each point represents a pair of "X" and "Y" values −

import matplotlib.pyplot as plt
# Enabling XKCD mode
plt.xkcd()
# Creating an XKCD style scatter plot
plt.scatter([1, 2, 3, 4], [1, 4, 9, 16], color='r')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('XKCD Style Scatter Plot')
plt.show()

Output

Output of the above code is as follows −

XKCD Style Scatter Plot

XKCD Style Bar Plot

In Matplotlib, an XKCD style bar plot is a type of chart where rectangular bars represent data values. When XKCD mode is enabled with plt.xkcd(), the bars and axes take on a playful and hand-drawn appearance, resembling doodles rather than precise graphical elements.

Bar plots are commonly used to compare categorical data or show the distribution of a dataset across different categories, and the XKCD style adds a fun twist to this traditional chart type.

Example

In the example below, we are using the XKCD mode to create a bar plot with four categories (A, B, C, D) and their corresponding values. We draw the bars in "green" color −

import matplotlib.pyplot as plt
# Enabling XKCD mode
plt.xkcd()

# Creating an XKCD style bar plot
plt.bar(['A', 'B', 'C', 'D'], [10, 20, 15, 25], color='g')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('XKCD Style Bar Plot')
plt.show()

Output

After executing the above code, we get the following output −

XKCD Style Bar Plot

XKCD Style Pie Chart

In Matplotlib, an XKCD style pie chart is a graphical representation of data where a circle is divided into slices to show numerical proportions. When XKCD mode is activated with plt.xkcd(), the pie chart takes on a hand-drawn appearance, resembling a sketch rather than a precise diagram. Each slice of the pie appears as if it were drawn by hand.

Pie charts are commonly used to display the composition of a dataset or the distribution of categories, and the XKCD style adds a fun twist to this classic chart type.

Example

Now, we are using the XKCD mode to create a a pie chart displaying the proportions of different categories (A, B, C, D). The "autopct" parameter adds percentage labels to each wedge, while "startangle" sets the starting angle for the first wedge −

import matplotlib.pyplot as plt
# Enabling XKCD mode
plt.xkcd()

# Creating an XKCD style pie chart
sizes = [15, 30, 45, 10]
plt.pie(sizes, labels=['A', 'B', 'C', 'D'], autopct='%1.1f%%', startangle=140)
plt.title('XKCD Style Pie Chart')
plt.show()

Output

The output obtained is as shown below −

XKCD Style Pie Chart
Advertisements