Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to produce a barcode in Matplotlib?
To produce a barcode in Matplotlib, you can create a visual representation using binary data (0s and 1s) where 1s represent black bars and 0s represent white spaces. This technique uses imshow() to display the binary pattern as a barcode-like image.
Basic Barcode Creation
Here's how to create a simple barcode using a binary array ?
import matplotlib.pyplot as plt
import numpy as np
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Binary pattern for barcode (1 = black bar, 0 = white space)
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
])
# Create figure with specified DPI
fig = plt.figure(dpi=100)
# Add axes that fill the entire figure
ax = fig.add_axes([0, 0, 1, 1])
ax.set_axis_off()
# Display the barcode using imshow
ax.imshow(code.reshape(1, -1), cmap='binary', aspect='auto',
interpolation='nearest')
plt.show()
Creating a More Realistic Barcode
For a more barcode-like appearance, you can adjust the aspect ratio and add labels ?
import matplotlib.pyplot as plt
import numpy as np
# Create a shorter, more realistic barcode pattern
barcode_data = np.array([1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1,
0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0])
fig, ax = plt.subplots(figsize=(8, 2))
# Create barcode image
ax.imshow(barcode_data.reshape(1, -1), cmap='binary', aspect='auto')
# Remove axes and add title
ax.set_xticks([])
ax.set_yticks([])
ax.set_title('Sample Barcode', fontsize=12, pad=10)
# Add barcode number below
plt.figtext(0.5, 0.1, '1234567890123', ha='center', fontsize=10,
fontfamily='monospace')
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Purpose | Common Values |
|---|---|---|
cmap='binary' |
Black and white colormap | 'binary', 'gray' |
aspect='auto' |
Automatic aspect ratio | 'auto', 'equal' |
interpolation='nearest' |
Sharp edges for bars | 'nearest', 'none' |
reshape(1, -1) |
Convert to 1D horizontal array | (1, n) for horizontal |
Customizing Barcode Appearance
You can modify colors and styling for different barcode types ?
import matplotlib.pyplot as plt
import numpy as np
# Generate random barcode pattern
np.random.seed(42)
barcode_pattern = np.random.choice([0, 1], size=50, p=[0.4, 0.6])
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 4))
# Standard black and white barcode
ax1.imshow(barcode_pattern.reshape(1, -1), cmap='binary', aspect='auto')
ax1.set_title('Standard Barcode')
ax1.set_xticks([])
ax1.set_yticks([])
# Colored barcode
ax2.imshow(barcode_pattern.reshape(1, -1), cmap='Blues', aspect='auto')
ax2.set_title('Colored Barcode')
ax2.set_xticks([])
ax2.set_yticks([])
plt.tight_layout()
plt.show()
Conclusion
Creating barcodes in Matplotlib involves using imshow() with binary arrays and the 'binary' colormap. The reshape(1, -1) method converts the data into a horizontal barcode format, while aspect='auto' ensures proper bar proportions.
Advertisements
