条形码示例

该演示展示了如何生成一维图像或“条形码”。

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. # Fixing random state for reproducibility
  4. np.random.seed(19680801)
  5. # the bar
  6. x = np.where(np.random.rand(500) > 0.7, 1.0, 0.0)
  7. axprops = dict(xticks=[], yticks=[])
  8. barprops = dict(aspect='auto', cmap=plt.cm.binary, interpolation='nearest')
  9. fig = plt.figure()
  10. # a vertical barcode
  11. ax1 = fig.add_axes([0.1, 0.3, 0.1, 0.6], **axprops)
  12. ax1.imshow(x.reshape((-1, 1)), **barprops)
  13. # a horizontal barcode
  14. ax2 = fig.add_axes([0.3, 0.1, 0.6, 0.1], **axprops)
  15. ax2.imshow(x.reshape((1, -1)), **barprops)
  16. plt.show()

条形码示例

参考

此示例中显示了以下函数,方法和类的使用:

  1. import matplotlib
  2. matplotlib.axes.Axes.imshow
  3. matplotlib.pyplot.imshow

下载这个示例