【问题标题】:How do i show the percentage number of a pie chart in turtle graphics inside the chart?如何在图表内的海龟图形中显示饼图的百分比数?
【发布时间】:2019-11-12 09:45:07
【问题描述】:

我的任务要求我从一个文本文件中打印出饼图上每个大陆 2018 年和 2019 年的人口差异。

饼图我已经做好了。

但是我想将饼图每个部分的百分比添加到饼图本身中。

目前,我有饼图之外的百分比数字。我该怎么做?

目前我的百分比数字在条形图之外,还有大洲名称:

def operation_1():
    divisions = [(('Asia',x1_p,"%"), x1), (('North America',x2_p,"%"), x2), (('South America',x4_p,"%"), x4), (('Africa',x5_p,"%"), x5), (('Europe',x6_p,"%"), x6), (('Oceania',x7_p,"%"), x7)]
    COLORS = cycle(['red', 'blue', 'green', 'yellow', 'orange', 'brown', 'lightgreen'])
    RADIUS = 200
    CONTINENT_RADIUS = RADIUS * 1.35
    PERCENTAGE_RADIUS = RADIUS * 1.35
    FONTSIZE = 15
    FONT = ("Arial", FONTSIZE, "bold")

    total = sum(fraction for fillcolor, fraction in divisions)
    screen = turtle.getscreen()
    screen.title("Pie Chart")
    screen.setworldcoordinates(-400, -400, 400,400 )
    piechart = Turtle()
    piechart.penup()
    piechart.sety(-RADIUS)
    piechart.pendown()
    piechart.speed(0)

    for fillcolor, fraction in divisions:
        piechart.fillcolor(next(COLORS))
        piechart.begin_fill()
        piechart.circle(RADIUS, fraction * 360 / total)
        position = piechart.position()
        piechart.goto(0,0)
        piechart.end_fill()
        piechart.setposition(position)

    piechart.penup()
    piechart.sety(-CONTINENT_RADIUS)

    for continent, fraction in divisions:
        piechart.circle(CONTINENT_RADIUS, fraction * 360 / total / 2)
        piechart.write(continent, align="center", font=FONT)
        piechart.circle(CONTINENT_RADIUS, fraction * 360 / total / 2)

    piechart.hideturtle()


    x = input("Please input x to initiate another operation: ")
    if x.lower() == 'x':
        screen.clearscreen()

【问题讨论】:

    标签: python function turtle-graphics


    【解决方案1】:

    您只需要调整您的PERCENTAGE_RADIUS 并在编写文本时设置它。带有随机百分比的完整示例:

    from turtle import *
    from itertools import cycle
    
    continent = ['Asia', 'North America', 'South America', 'Africa', 'Europe', 'Oceania']
    percentage = [10, 5, 20, 10, 25, 5, 25]
    divisions = [('{:s} {:d}%'.format(c, p), p) for c, p in zip(continent, percentage)]
    
    def operation_1(divisions):
        COLORS = cycle(['red', 'blue', 'green', 'yellow', 'orange', 'brown', 'lightgreen'])
        RADIUS = 200
        CONTINENT_RADIUS = RADIUS * 1.35
        PERCENTAGE_RADIUS = RADIUS * 0.8 # Set your radius here
        FONTSIZE = 15
        FONT = ("Arial", FONTSIZE, "bold")
    
        total = sum(fraction for fillcolor, fraction in divisions)
        screen = getscreen()
        screen.title("Pie Chart")
        screen.setworldcoordinates(-400, -400, 400,400 )
        piechart = Turtle()
        piechart.penup()
        piechart.sety(-RADIUS)
        piechart.pendown()
        piechart.speed(0)
    
        for _, fraction in divisions:
            piechart.fillcolor(next(COLORS))
            piechart.begin_fill()
            piechart.circle(RADIUS, fraction * 360 / total)
            position = piechart.position()
            piechart.goto(0,0)
            piechart.end_fill()
            piechart.setposition(position)
    
        piechart.penup()
        piechart.sety(-PERCENTAGE_RADIUS)
    
        for continent, fraction in divisions:
            # Use the text radius here
            piechart.circle(PERCENTAGE_RADIUS, fraction * 360 / total / 2)
            piechart.write(continent, align="center", font=FONT)
            # Use the text radius here
            piechart.circle(PERCENTAGE_RADIUS, fraction * 360 / total / 2)
    
        piechart.hideturtle()
    
    
        x = input("Please input x to initiate another operation: ")
        if x.lower() == 'x':
            screen.clearscreen()
    
    
    operation_1(divisions)
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多