Home »
Python »
Python Data Visualization
Python | Bar Code
In this tutorial, we are going to learn how to generate a random barcode in Python using matplotlib?
Submitted by Anuj Singh, on August 20, 2020
Illustrations:
Python code for bar code
import matplotlib.pyplot as plt
import numpy as np
# the bar
x = np.random.rand(500) > 0.7
fig = plt.figure()
# a barcode
ax = fig.add_axes([0.3, 0.4, 0.6, 0.2])
ax.set_axis_off()
ax.imshow(x.reshape((1, -1)), aspect='auto',
cmap='binary', interpolation='nearest')
ax.set_title('Barcode Example 1')
plt.show()
##########################################
##########################################
x = np.random.rand(500) > 0.7
fig = plt.figure()
# a barcode
ax = fig.add_axes([0.3, 0.4, 0.6, 0.2])
ax.set_axis_off()
ax.imshow(x.reshape((1, -1)), aspect='auto',
cmap='binary', interpolation='nearest')
ax.set_title('Barcode Example 2')
plt.show()
##########################################
##########################################
x = np.random.rand(500) > 0.7
fig = plt.figure()
# a barcode
ax = fig.add_axes([0.3, 0.4, 0.6, 0.2])
ax.set_axis_off()
ax.imshow(x.reshape((1, -1)), aspect='auto',
cmap='binary', interpolation='nearest')
ax.set_title('Barcode Example 3')
plt.show()
Output:
Output is as Figure