Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb02fedb

Browse files
committed
Rewrite the barcode example
This is a bit particular anyway, but let's make it practical bygenerating a real bar code and not only some random lines.Fun fact: The used code is the GTIN-13 for "The Visual Display ofQuantitative Information", Edward R. Tufte.
1 parent278cdbe commitb02fedb

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed
Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
"""
2-
============
3-
Barcode Demo
4-
============
5-
6-
This demo shows how to produce a one-dimensional image, or "bar code".
2+
=======
3+
Barcode
4+
=======
5+
This demo shows how to produce a bar code.
6+
7+
The figure is calculated so that the width in pixel is a multiple of the
8+
number of data points to prevent interpolation artifacts. Additionally, the
9+
axes is defined to span the whole figure and axis are turned off.
10+
11+
The data itself is rendered with `~.Axes.imshow` using
12+
13+
- ``code.reshape(1, -1)`` to turn the data into a 2D array with one row.
14+
- ``imshow(..., aspect='auto')`` to allow for non-square pixels.
15+
- ``imshow(..., interpolation='nearest')`` to prevent blurred edges. This
16+
should not happen anyway because we fine-tuned the figure width in pixels,
17+
but just to be save.
718
"""
19+
820
importmatplotlib.pyplotasplt
921
importnumpyasnp
1022

11-
# Fixing random state for reproducibility
12-
np.random.seed(19680801)
13-
14-
# the bar
15-
x=np.random.rand(500)>0.7
16-
17-
barprops=dict(aspect='auto',cmap='binary',interpolation='nearest')
18-
19-
fig=plt.figure()
2023

21-
# a vertical barcode
22-
ax1=fig.add_axes([0.1,0.1,0.1,0.8])
23-
ax1.set_axis_off()
24-
ax1.imshow(x.reshape((-1,1)),**barprops)
24+
code=np.array([
25+
1,0,1,0,1,1,1,0,1,1,0,0,0,1,0,0,1,0,1,0,0,1,1,1,
26+
0,0,0,1,0,1,1,0,0,0,0,1,0,1,0,0,1,1,0,0,1,0,1,0,
27+
1,0,1,0,0,0,0,1,0,1,1,1,0,1,0,0,1,1,0,1,1,0,0,1,
28+
1,0,0,1,1,0,1,0,1,1,1,0,0,1,0,0,0,1,0,0,1,0,1])
2529

26-
# a horizontal barcode
27-
ax2=fig.add_axes([0.3,0.4,0.6,0.2])
28-
ax2.set_axis_off()
29-
ax2.imshow(x.reshape((1,-1)),**barprops)
30+
pixel_per_bar=4
31+
dpi=100
3032

33+
fig=plt.figure(figsize=(len(code)*pixel_per_bar/dpi,2),dpi=dpi)
34+
ax=fig.add_axes([0,0,1,1])# span the whole figure
35+
ax.set_axis_off()
36+
ax.imshow(code.reshape(1,-1),cmap='binary',aspect='auto',
37+
interpolation='nearest')
3138
plt.show()
3239

3340
#############################################################################
@@ -43,3 +50,4 @@
4350
importmatplotlib
4451
matplotlib.axes.Axes.imshow
4552
matplotlib.pyplot.imshow
53+
matplotlib.figure.Figure.add_axes

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp