如何在 Matplotlib 中生成條形碼?


要在 Matplotlib 中生成條形碼,我們可以執行下列步驟:

  • 設定圖片大小並調整子圖之間的和周圍的邊距。
  • 列出二進位制數,即 0 和 1。
  • 使用dpi=100建立新影像或啟用現有影像。
  • 向影像新增一個座標軸。
  • 關閉座標軸。
  • 使用imshow()方法繪製步驟 2 中的資料。
  • 要顯示圖片,請使用show()方法。

示例

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

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])

fig = plt.figure(dpi=100)

ax = fig.add_axes([0, 0, 1, 1])
ax.set_axis_off()

ax.imshow(code.reshape(1, -1), cmap='binary', aspect='auto',
         interpolation='nearest')

plt.show()

輸出

更新日期:2021 年 6 月 18 日

299 次瀏覽

開啟你的 職業

完成課程即可獲得認證

開始學習
廣告
© . All rights reserved.