Create Barcode with Matplotlib

PythonPythonBeginner
Practice Now

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

Introduction

This lab demonstrates how to create a barcode using Matplotlib in Python. The barcode is produced using a binary array of ones and zeros, and is rendered using Axes.imshow.

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.

Import the Required Libraries

We first need to import the necessary libraries, including numpy and matplotlib.

import matplotlib.pyplot as plt
import numpy as np

Create the Binary Array

Next, we need to create the binary array that will be used to generate the barcode. In this example, we will use the following binary array:

code = np.array([
    1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1,
    0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0,
    1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1,
    1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1])

Set the Pixel and DPI Values

We need to define the pixel and DPI values for the barcode. In this example, we will use a pixel value of 4 and a DPI value of 100.

pixel_per_bar = 4
dpi = 100

Create the Figure and Axes

We need to create the figure and axes for the barcode. We will set the figure size to a multiple of the number of data points, and turn off all axis.

fig = plt.figure(figsize=(len(code) * pixel_per_bar / dpi, 2), dpi=dpi)
ax = fig.add_axes([0, 0, 1, 1])  ## span the whole figure
ax.set_axis_off()

Render the Barcode

Finally, we can render the barcode using Axes.imshow. We will use code.reshape(1, -1) to turn the data into a 2D array with one row, imshow(..., aspect='auto') to allow for non-square pixels, and imshow(..., interpolation='nearest') to prevent blurred edges.

ax.imshow(code.reshape(1, -1), cmap='binary', aspect='auto',
          interpolation='nearest')
plt.show()

Summary

In this lab, we learned how to create a barcode using Matplotlib in Python. We first imported the necessary libraries, then created a binary array that will be used to generate the barcode. We then set the pixel and DPI values and created the figure and axes. Finally, we rendered the barcode using Axes.imshow.

Other Python Tutorials you may like