piegraph.py

Created by jlevon

Created on September 10, 2024

1.12 KB

Statistiques : Permet de créer un graphique camembert suivant les données saisies


from turtle import *

# Data for the pie chart
sizes = [15, 30, 45, 10]  # Percentages for each category
labels = ['15%', '30%', '45%', '10%']  # Corresponding percentage labels

# Function to draw a pie chart slice
def draw_slice(pct, label):
    angle = pct * 360 / 100  # Convert percentage to angle
    
    penup()
    forward(80)  # Move to the circumference of the larger circle
    left(90)
    pendown()
    circle(80, angle)  # Draw the arc with the new larger radius
    left(90)
    forward(80)  # Move back to the center
    left(90)
    penup()
    forward(120)  # Move further out to write the label outside the pie
    write(label)
    backward(120)  # Move back to the center
    left(90)
    pendown()

    # Reset orientation for the next slice
    right(90 + angle)  # Rotate to the starting position for the next slice

# Initialize turtle
speed(0)  # Max speed for drawing
penup()
goto(0, 0)  # Start at the bottom of the larger circle to center it better
pendown()

# Draw each slice
for i in range(len(sizes)):
    draw_slice(sizes[i], labels[i])

# Hide the turtle after completing the pie chart
hideturtle()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.