#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Radar Spider Chart - Python.


    I have a Uni assignment I am currently stuck on. So far my code looks like this:

    #-----Task Description-----------------------------------------------#
    #
    # RADAR CHART
    #
    # Radar charts are a common way of visualising data comprising
    # several values of different types. In this task you and your
    # programming partner will develop a Python function that uses
    # Turtle graphics to draw a radar chart given a list of data
    # values. See the instruction sheet accompanying this file for
    # full details.
    #
    #--------------------------------------------------------------------#



    #-----Students' Solution---------------------------------------------#
    #
    # Complete the task by filling in the template below.


    ##### PUT YOUR radar_chart FUNCTION HERE


    #
    #--------------------------------------------------------------------#
    import turtle

    def radar_chart(plist, chartsize):
    ## check to see if the data list is required length
    line_number = len(plist)
    if line_number < 3:
    return

    ##full circle is broken up into sections determined by number
    ##of pieces of data
    angle = 360/(line_number)
    ##allows user to change size of graph
    chartsize -= 100
    ##makes the turtle face up
    turtle.left(90)
    ## initialise list of positions for turtle to draw lines between
    ## at the end of each spoke
    positionlist = []
    datalist = []

    ## goes through perclist and draws a spoke for each element of data,
    ## chartsize long, appends the position list with its position,
    ## then heads back for the center
    for i in plist:
    turtle.forward(i)
    datalist.append(turtle.position())
    turtle.forward(chartsize-i)
    positionlist.append(turtle.position())
    turtle.penup()
    turtle.back(chartsize)
    turtle.right(angle)
    turtle.pendown()


    ## goes to the first spot in positionlist
    turtle.penup()
    turtle.goto(positionlist[0])
    turtle.pendown()

    ## goes through positionlist, drawing the lines between
    for i, h in enumerate(positionlist):
    if i < len(positionlist)-1:
    turtle.goto(positionlist[i+1])
    else:
    continue

    turtle.goto(positionlist[0])


    ## puts turtle on the first data dot
    turtle.penup()
    goto(datalist[0])
    pendown()

    ## starts to draw lines between data dots
    turtle.fill(True)
    for i in datalist:
    if i < len(datalist)-1:
    turtle.goto(datalist[0])

    turtle.dot(10, "blue")
    ## Errors here, not sure why for the moment: turtle.goto(datalist[i+1])
    turtle.fill(False)


    #-----Testing--------------------------------------------------------#
    #
    # Below is some data to use when testing your function, including
    # the example shown in the instruction sheet. When we mark your
    # solution we may use tests other than these, so make sure that
    # your function works for any given data set.
    #

    # Some "typical" test data
    test1 = [32, 56, 100, 78, 23, 90, 0, 12, 45, 90, 23]
    test2 = [88, 76, 63, 84, 98, 10, 17, 82, 65]
    test3 = [32, 61, 15, 95, 24]
    test4 = [60, 97, 86, 39, 67, 22, 62, 58, 20, 37, 65,
    72, 34, 67, 64, 56, 50, 32, 50, 93, 68]

    # Some "extreme" test data
    test5 = [25, 75, 25] # a triangle (the shortest "reasonable" list)
    test6 = [100, 100, 100, 100] # produces a filled square

    # Run a test - change the test number for different charts
    #speed('fastest') # draw quickly
    radar_chart(test1, 200)
    # call the function

    #
    #--------------------------------------------------------------------#

    It's meant to draw a radar then with the tests provided, plot them onto the radar... could someone please help me out as I am really new to python and don't know how to advance from where I am at.

    Thanks
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,896
    Rep Power
    481
    Lesson 1: python syntax includes indentation white space. Please obey the instructions at my signature for valid python posts.
    Code:
    #-----Task Description-----------------------------------------------#
    #
    #  RADAR CHART
    #
    #  Radar charts are a common way of visualising data comprising
    #  several values of different types.  In this task you and your
    #  programming partner will develop a Python function that uses
    #  Turtle graphics to draw a radar chart given a list of data
    #  values.  See the instruction sheet accompanying this file for
    #  full details.
    #
    #--------------------------------------------------------------------#
    
    
    
    #-----Students' Solution---------------------------------------------#
    #
    #  Complete the task by filling in the template below.
    
    
    ##### PUT YOUR radar_chart FUNCTION HERE
    
    
    #
    #--------------------------------------------------------------------#
    import turtle 
    
    def radar_chart(plist, chartsize):
        ## check to see if the data list is required length
        line_number = len(plist)
        if line_number < 3:
            return 
    
        ##full circle is broken up into sections determined by number
        ##of pieces of data
        angle = 360/(line_number)
        ##allows user to change size of graph
        chartsize -= 100
        ##makes the turtle face up
        turtle.left(90)
        ## initialise list of positions for turtle to draw lines between
        ## at the end of each spoke
        positionlist = []
        datalist = []
    
        ## goes through perclist and draws a spoke for each element of data,
        ## chartsize long, appends the position list with its position,
        ## then heads back for the center
        for i in plist:
            turtle.forward(i)
            datalist.append(turtle.position())
            turtle.forward(chartsize-i)
            positionlist.append(turtle.position())
            turtle.penup()
            turtle.back(chartsize)
            turtle.right(angle)
            turtle.pendown()
    
    
        ## goes to the first spot in positionlist
        turtle.penup()
        turtle.goto(positionlist[0])
        turtle.pendown()
    
        ## goes through positionlist, drawing the lines between
        for i, h in enumerate(positionlist):
            if i < len(positionlist)-1:
                turtle.goto(positionlist[i+1])
            else:
                continue
    
            turtle.goto(positionlist[0])
        
    
        ## puts turtle on the first data dot
            turtle.penup()
            goto(datalist[0])
            pendown()
        
        ## starts to draw lines between data dots
            turtle.fill(True)
        for i in datalist:
            if i < len(datalist)-1:
                turtle.goto(datalist[0])
    
                turtle.dot(10, "blue")
            ## Errors here, not sure why for the moment: turtle.goto(datalist[i+1])
                turtle.fill(False)
    
    
    #-----Testing--------------------------------------------------------#
    #
    #  Below is some data to use when testing your function, including
    #  the example shown in the instruction sheet.  When we mark your
    #  solution we may use tests other than these, so make sure that
    #  your function works for any given data set.
    #
    
    # Some "typical" test data
    test1 = [32, 56, 100, 78, 23, 90, 0, 12, 45, 90, 23]
    test2 = [88, 76, 63, 84, 98, 10, 17, 82, 65]
    test3 = [32, 61, 15, 95, 24]
    test4 = [60, 97, 86, 39, 67, 22, 62, 58, 20, 37, 65,
             72, 34, 67, 64, 56, 50, 32, 50, 93, 68]
    
    # Some "extreme" test data
    test5 = [25, 75, 25] # a triangle (the shortest "reasonable" list) 
    test6 = [100, 100, 100, 100] # produces a filled square
    
    # Run a test - change the test number for different charts
    #speed('fastest') # draw quickly
    radar_chart(test1, 200)
    # call the function
    
    #
    #--------------------------------------------------------------------#
    I won't get back to this before Saturday, if ever.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    509
    Rep Power
    33
    Obviously you did not print out "i" (not a good choice for a variable as i, l and o can look like numbers).
    Code:
        for i in datalist:
            ## Errors here, not sure why for the moment: turtle.goto(datalist[i+1])

IMN logo majestic logo threadwatch logo seochat tools logo