{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Barcode\nThis demo shows how to produce a bar code.\n\nThe figure size is calculated so that the width in pixels is a multiple of the\nnumber of data points to prevent interpolation artifacts. Additionally, the\n``Axes`` is defined to span the whole figure and all ``Axis`` are turned off.\n\nThe data itself is rendered with `~.Axes.imshow` using\n\n- ``code.reshape(1, -1)`` to turn the data into a 2D array with one row.\n- ``imshow(..., aspect='auto')`` to allow for non-square pixels.\n- ``imshow(..., interpolation='nearest')`` to prevent blurred edges. This\n should not happen anyway because we fine-tuned the figure width in pixels,\n but just to be safe.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\nimport numpy as np\n\ncode = np.array([\n 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1,\n 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0,\n 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1,\n 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1])\n\npixel_per_bar = 4\ndpi = 100\n\nfig = plt.figure(figsize=(len(code) * pixel_per_bar / dpi, 2), dpi=dpi)\nax = fig.add_axes([0, 0, 1, 1]) # span the whole figure\nax.set_axis_off()\nax.imshow(code.reshape(1, -1), cmap='binary', aspect='auto',\n interpolation='nearest')\nplt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.axes.Axes.imshow` / `matplotlib.pyplot.imshow`\n - `matplotlib.figure.Figure.add_axes`\n\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}