画一个类似于条形码的图

例如我现在有一个list=[1,2,3,4,2,4,2,3,2,1,2,3,2,1,2,3,2,1,4],如你所见,list中只有1,2,3,4四种元素,我现在想做的是将每一种元素对应一种不同颜色的细柱图画出来,各个柱的颜色不同大小相同,类似下图,只不过条形码颜色顺序是根据list中元素的顺序画,谢谢!!图片描述

回答:

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存放颜色代码

回答:

自己写一个映射,比如这样

{

valus:1,

color:#999999, //手机码字,伪代码,格式自己改一下

weight:2px

}

然后遍历list,取出映射值赋给style就行了

以上是 画一个类似于条形码的图 的全部内容, 来源链接: utcz.com/a/158359.html

回到顶部