## LEDBarGraph >![](https://img.kancloud.cn/79/f1/79f10c1f13d12c8b83bc2fcb25eaf9f3_512x399.png) 左上角的条状图就是这个类要实现的效果了,就是有若干个指示灯,用亮度来表示不同的范围。微软为microbit写的图形化编辑器makecode中也有类似的积木块: ![](https://img.kancloud.cn/66/f4/66f4754110be8c0549c043a69817f0db_929x498.png) 利用`LEDBarGraph`类,我们可以把若干个LED灯的集合看做是一个条形图。 ~~~ from gpiozero import LEDBarGraph from time import sleep from __future__ import division # required for python 2 graph = LEDBarGraph(5, 6, 13, 19, 26, 20) graph.value = 1 # (1, 1, 1, 1, 1, 1) sleep(1) graph.value = 1/2 # (1, 1, 1, 0, 0, 0) sleep(1) graph.value = -1/2 # (0, 0, 0, 1, 1, 1) sleep(1) graph.value = 1/4 # (1, 0, 0, 0, 0, 0) sleep(1) graph.value = -1 # (1, 1, 1, 1, 1, 1) sleep(1) ~~~ 虽然本质上当`pwm=False`(默认)的时候,LED只有开和关两种状态。 但是在使用`LEDBarGraph`的时候,如果我们把pwm设置为真`pwm=True`,那么利用LED亮度来表示条形图会更加精确。 ~~~ from gpiozero import LEDBarGraph from time import sleep from __future__ import division # required for python 2 graph = LEDBarGraph(5, 6, 13, 19, 26, pwm=True) graph.value = 1/10 # (0.5, 0, 0, 0, 0) sleep(1) graph.value = 3/10 # (1, 0.5, 0, 0, 0) sleep(1) graph.value = -3/10 # (0, 0, 0, 0.5, 1) sleep(1) graph.value = 9/10 # (1, 1, 1, 1, 0.5) sleep(1) graph.value = 95/100 # (1, 1, 1, 1, 0.75) sleep(1) ~~~