Python 甲鱼的标记片段

Python 甲鱼的标记片段,python,label,labels,turtle-graphics,Python,Label,Labels,Turtle Graphics,下面是使用Python的Turtle 我正试图在我用Turtle制作的饼图上的相应部分旁边放置预设标签(位于列表中)。这些数字改变了图表,所以我需要的标签不是在一个地方,而是根据饼图的每个部分有多大而移动 这就是我被困的地方。我是否应该使用def()使每组列表只使用一个函数进行更改? 到目前为止,我有: groups = ['Group A', 'Group B', 'Group C', 'Group D'] percentages = [0.2, 0.4, 0.15, 0.25] def l

下面是使用Python的Turtle

我正试图在我用Turtle制作的饼图上的相应部分旁边放置预设标签(位于列表中)。这些数字改变了图表,所以我需要的标签不是在一个地方,而是根据饼图的每个部分有多大而移动

这就是我被困的地方。我是否应该使用def()使每组列表只使用一个函数进行更改? 到目前为止,我有:

groups = ['Group A', 'Group B', 'Group C', 'Group D']
percentages = [0.2, 0.4, 0.15, 0.25]

def label():
    penup()
    for percent in percentages:
        setheading((percentages[0] * 360)/2)
        forward(radius + 20)
        color("Black")
        write(groups[0], False, "left", font=("Arial", 15, "bold"))
        end_fill() 
label()
我计划每个小组有4个。但是,如果小组中有4人以上呢?我认为它是循环的,因为它每20像素打印第一个组名(组A)。如果能朝着正确的方向努力,我们将不胜感激


它看起来是什么样子的图片:

嗯,它正朝着对角线方向移动,因为在你的循环结束时,你从未让海龟走到循环的中间

设定航向不会让海龟朝着你所希望的方向前进——在360度的圆上设定航向点为度。0=东,90=北,180=西,270=南

你想让你的海龟在每一个百分比值。我会通过在
开始之前开始有一个值,例如
newheading=0
来表示百分比百分比:
这样,您可以将
((百分比[0]*360)/2)
更改为
((百分比[newheading]*360)/2)

当然,您需要知道如何在每次通过循环时向
newheading
值添加值。希望这条建议能让你思考如何做这件事。

关于第一篇文章。如何使所有组同时出现组=['Group A'、'Group B'、'Group C'、'Group D']

我将通过采用整个饼图来解决标签问题,因为我认为不应该有单独的
百分比
列表,而是标签和切片百分比的一个组合数据结构。以下内容分三次绘制了一个饼图——通过将切片一分为二并在中心标记,将标签写在半径更大的假想圆的边缘上:

from turtle import Turtle, Screen
from operator import itemgetter

FONT = ("Arial", 15, "bold")
LABEL_OFFSET = 1.33

pie_slices = {'Group A': 0.2, 'Group B': 0.4, 'Group C': 0.15, 'Group D': 0.25}

def pie_chart(turtle, radius, slices, font):

    # Pass 1: the pie itself
    x, y = turtle.position()
    turtle.penup()
    turtle.sety(y - radius)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(radius, steps=60)  # steps setting improves smaller charts
    turtle.end_fill()
    turtle.penup()

    # Pass 2: the slices
    for _, percentage in slices:
        turtle.circle(radius, extent=360 * percentage)
        position = turtle.position()
        turtle.pendown()
        turtle.goto(x, y)
        turtle.penup()
        turtle.goto(position)

    # Pass 3: the labels
    radius *= LABEL_OFFSET
    turtle.sety(y - radius)
    for label, percentage in slices:
        turtle.circle(radius, extent=180 * percentage)
        # should do smarter label alignment to reduce LABEL_OFFSET
        turtle.write(label, align="center", font=font)
        turtle.circle(radius, extent=180 * percentage)


yertle = Turtle(visible=False)
yertle.color("black", "orange")
yertle.speed("fastest")
yertle.width(2)

# pie_chart() expects list of pairs, sort our dictionary by slice size into list
sorted_slices = sorted(pie_slices.items(), key=itemgetter(1), reverse=True)

pie_chart(yertle, 150, sorted_slices, FONT)

yertle.hideturtle()

screen = Screen()
screen.exitonclick()
此代码的行为在某种程度上取决于如何设置切片(已排序、未排序)以及如何预先设置海龟(笔颜色、填充颜色、宽度、初始位置、速度、可见性等)

输出


是的,我设法更新了您建议的变量,谢谢!。关于设置标题,我应该将以度为单位的百分比添加到之前的设置标题中,还是有更简单的方法来计算放置标签的角度?您可以这样做:
half_angle=percent*360/2
,然后在循环开始时执行标签代码,然后
goto(0,0)
然后通过
lt(半角)
完成剩下的角度,这样,每一个循环,它在饼图的每个角度移动一半,从饼图中弹出,写下标签,头回到中间,转向饼图的另一半,然后重新开始下一个循环,以获得下一个值。