Python Forum
Turtle Bar Chart Colour fill question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turtle Bar Chart Colour fill question
#1
Hey,
I am using Turtle to learn how if and elif statements work.
I have a quick program that draws a bar chart... here it is...

import turtle

john = turtle.Turtle()
xs = [48, 117, 200, 240, 160, 260, 220]  #values for the heights of the bars

def draw_bar(t, height):
    """ Get turtle t to draw one bar, of height. """
    t.begin_fill()
    if v >=200:
        john.color("blue", "red")
    elif v >=100:
        john.color("blue", "yellow")
    else:
        john.color("blue", "green")
    t.left(90)
    t.forward(height)# Draw up the left side
    t.write("  "+ str(height))
    t.right(90)
    t.forward(40)         # Width of bar, along the top
    t.right(90)
    t.forward(height)     # And down again!
    t.left(90)            # Put the turtle facing the way we found it.
    t.end_fill()
    t.penup()
    t.forward(10)         # Leave small gap after each bar
    t.pendown()

wn = turtle.Screen()         # Set up the window and its attributes
wn.bgcolor("lightgreen")

# Create john and set some attributes

john.pensize(3)

for v in xs:              
    draw_bar(john, v)
My question is, why does putting the condition for the bar fill colours work where it is now and not where I placed it the first time (I had it when I called the function)... like this...

import turtle

john = turtle.Turtle()
xs = [48, 117, 200, 240, 160, 260, 220]  #values for the heights of the bars

def draw_bar(t, height):
    """ Get turtle t to draw one bar, of height. """
    t.begin_fill()
    t.left(90)
    t.forward(height)# Draw up the left side
    t.write("  "+ str(height))
    t.right(90)
    t.forward(40)         # Width of bar, along the top
    t.right(90)
    t.forward(height)     # And down again!
    t.left(90)            # Put the turtle facing the way we found it.
    t.end_fill()
    t.penup()
    t.forward(10)         # Leave small gap after each bar
    t.pendown()

wn = turtle.Screen()         # Set up the window and its attributes
wn.bgcolor("lightgreen")

# Create john and set some attributes

john.pensize(3)


for v in xs:              
    draw_bar(john, v)
    if v >=200:
        john.color("blue", "red")
    elif v >=100:
        john.color("blue", "yellow")
    else:
        john.color("blue", "green")
Does that make sense??
Reply
#2
In the second script, you appear to be setting the color, but not drawing anything
Reply
#3
When I run the second script it draws the bars but the fill colours are wrong! The first bar is filled in black!!
Reply
#4
You're setting the color after you draw the bar, you want to set it before you draw the bar. Whatever color the turtle has when it draws is what it draws. You don't set it after drawing to change what was just drawn.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 903 Oct-25-2023, 09:09 AM
Last Post: codelab
Question convert autohotkey script to python / macro - press key when pixel get colour willson94d 1 3,574 Jan-01-2022, 08:13 PM
Last Post: Yoriz
  Noob question: why is shapesize() not working for my turtle adifrank 8 5,662 Sep-09-2020, 11:13 PM
Last Post: adifrank
  Adding colour to Python console Archangelos 4 2,688 Apr-15-2020, 05:15 PM
Last Post: newbieAuggie2019
  Is it possible to have my python shell change the backgrounds colour? OTO1012 2 3,714 Mar-07-2019, 09:32 PM
Last Post: woooee
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 6,129 Feb-06-2019, 01:25 AM
Last Post: woooee
  Help! Turtle not working, even when we click the turtle demo in IDLE nothing happens. BertyBee 3 5,544 Jan-04-2019, 02:44 AM
Last Post: SheeppOSU
  Best way to set cell background colour in openpyxl Pedroski55 4 68,358 Aug-08-2018, 01:24 PM
Last Post: sumandas89
  I am trying to make a Colour Wars game, "Winning code" missing Ondrejoda 0 2,249 Feb-18-2018, 11:06 AM
Last Post: Ondrejoda
  Working with turtle graphics in python and colour slices of a pie graph... netrate 1 4,441 Feb-03-2018, 08:41 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020