使用Python的turtle模塊畫出簡單的柱狀圖


代碼如下:

import turtle
heights = [856, 420,360,260,205]
def main():
    t = turtle.Turtle()
    t.hideturtle()
    for i in range(5):
        drawFilledRectangle(t,-200+(76*i),0,76,heights[i]/4,"black","light blue")
    displayText(t)
def drawFilledRectangle(t,x,y,w,h,colorP="black",colorF="white"):
    t.pencolor(colorP)
    t.fillcolor(colorF)
    t.up()
    t.goto(x,y)
    t.down()
    t.begin_fill()
    t.goto(x+w,y)
    t.goto(x+w,y+h)
    t.goto(x,y+h)
    t.goto(x,y)
    t.end_fill()
def displayText(t):
    languages = ["haha1", "haha2", "haha3", "haha4", "haha5"]
    t.pencolor("blue")
    t.up()
    for i in range(5):
        t.goto((-162+76*i),heights[i] / 4)
        t.write(str(heights[i]),align="center",font=("Arial",10,"normal"))
        t.goto((-162+76*i),10)
        t.write(languages[i],align="center",font=("Arial",10,"normal"))
        t.goto(-200,-25)
        t.write("haha 統計圖",font=("Arial",10,"normal"))
        t.goto(-200,-45)
        t.write('(哈哈哈哈哈啊哈哈)',font=("Arial",10,"normal"))
main()

效果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM