python - 画一个类似于条形码的图
本文介绍了python - 画一个类似于条形码的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
例如我现在有一个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')
这篇关于python - 画一个类似于条形码的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文