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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/heatmaps("`Heatmaps`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48580{{"`Create Barcode with Matplotlib`"}} matplotlib/importing_matplotlib -.-> lab-48580{{"`Create Barcode with Matplotlib`"}} matplotlib/figures_axes -.-> lab-48580{{"`Create Barcode with Matplotlib`"}} matplotlib/heatmaps -.-> lab-48580{{"`Create Barcode with Matplotlib`"}} python/lists -.-> lab-48580{{"`Create Barcode with Matplotlib`"}} python/tuples -.-> lab-48580{{"`Create Barcode with Matplotlib`"}} python/importing_modules -.-> lab-48580{{"`Create Barcode with Matplotlib`"}} python/numerical_computing -.-> lab-48580{{"`Create Barcode with Matplotlib`"}} python/data_visualization -.-> lab-48580{{"`Create Barcode with Matplotlib`"}} python/build_in_functions -.-> lab-48580{{"`Create Barcode with Matplotlib`"}} end

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