我正在尝试编写一个函数,在给定每个条形长度的数组时绘制条形图。当我使用给定数组运行函数时,它将绘制条形图,并且所有条都用蓝色着色。我完成了那部分 下面是该部分的代码。 我遇到的问题是,我不知道如何在该图表中找出与最大和最小条形成不同颜色的方法。
def drawgraph2(volumes):
maxLength = maxValue2(volumes)
pict = makeEmptyPicture(700,(700/4)*3)
w = getWidth(pict)
h = getHeight(pict)
grey = makeColor(220,220,220)
x = 10
for n in range (0, len(volumes)):
addRectFilled(pict, x+100, (h-(maxLength/10))-volumes[n], 30, volumes[n], blue)
x = x+50
repaint(pict)
def maxValue2(trainsList):
max = trainsList[0]
for n in range(1, len(trainsList)):
if trainsList[n]>max:
max = trainsList[n]
return max
答案 0 :(得分:0)
我建议获取max和min(How to get min and max ids of a list)的ID。
minId = volumes.index(min(volumes))
maxId = volumes.index(max(volumes))
然后,您可以使用列表推导使用默认颜色填充矢量。
myColourList = ["blue" for i in range(len(volumes))]
并通过索引修改它;
myColourList[minId] = "red"
myColourList[maxId] = "green"
最后在循环中,您可以替换以下行:
addRectFilled(pict, x+100, (h-(maxLength/10))-volumes[n], 30, volumes[n], blue)
人:
addRectFilled(pict, x+100, (h-(maxLength/10))-volumes[n], 30, volumes[n], myColourList[n])