import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
l = list(range(4))
x = 0.03
for i in l:
if i == 0:
ax1.add_patch(patches.Rectangle((x, 0.1), 0.01, 0.6, facecolor=None))
if i == 1:
ax1.add_patch(
patches.Rectangle((x, 0.1), 0.01, 0.6, facecolor="green"))
if i == 2:
ax1.add_patch(patches.Rectangle((x, 0.1), 0.01, 0.6, facecolor="red"))
if i == 3:
ax1.add_patch(
patches.Rectangle((x, 0.1), 0.01, 0.6, facecolor="#00ffff"))
x += 0.012
fig1.savefig('barcode.png', dpi=90, bbox_inches='tight')
再创一个list存放颜色代码
自己写一个映射,比如这样
然后遍历list,取出映射值赋给style就行了