I am new to Python and I am struggling with a lesson in our book where we have to change the colors in a bar graph. I am not sure what I am doing wrong. There is no error message the colors are just printing black.
import turtle
tess = turtle.Turtle()
def draw_bar(t, height):
t.begin_fill()
t.left(90)
t.forward(height)
t.write(" "+ str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.penup()
t.left(90)
t.end_fill()
t.forward(10)
t.pendown()
tess.pensize(3)
if xs is 48:
tess.color("blue")
if xs is 117:
tess.color("yellow")
wn = turtle.Screen()
wn.bgcolor("lightgreen")
xs = [48,117,200,240,160,260,220]
for a in xs:
draw_bar(tess, a)
wn.mainloop()
This is what I have so far.
Thanks for the help!
I think you need to have a color assigned before you begin_fill(). Because the computer is like: "Well, I'll fill the box, but I don't have a color", so it uses the default of black.
The if statement you provided doesn't make sense, so I moved it up in the function and modified it where you can examine the height within the function. Instead of using if/else, I'd recommend dictionaries.
This new code works:
import turtle
tess = turtle.Turtle()
def draw_bar(t, height):
# Select Color
if height is 48:
color = "blue"
elif height is 117:
color = "yellow"
else:
color = "black"
# Assign color
t.color(color)
# The rest of your code
t.begin_fill()
t.left(90)
t.forward(height)
t.write(" "+ str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.penup()
t.left(90)
t.end_fill()
t.forward(10)
t.pendown()
tess.pensize(3)
wn = turtle.Screen()
wn.bgcolor("lightgreen")
xs = [48,117,200,240,160,260,220]
for a in xs:
draw_bar(tess, a)
wn.mainloop()
Related
Can anyone tell me, why there is a white space between the ground and the wall? I cant find the mistakte.
First it worked, but it seems like I changed amything and no i dont the what I did wrong. Thanks for helping!
Heres the code:
from turtle import Turtle, Screen
import random
from random import randint
screen = Screen ()
screen.setup (1920, 1080, 0, 0)
t = Turtle ()
t.color ("red")
t.speed (0)
s = Turtle()
s.color("black")
s.speed(0)
# Ego-Perspektive
t.up ()
t.goto (-950, -450)
t.down ()
t.color ("#DFCBAF")
t.begin_fill ()
t.goto(950,-450)
t.goto(999.01,-0.99)
t.goto(-400.99,-0.99)
t.goto(-950,-450)
t.end_fill()
t.color ("#d28c5b")
t.begin_fill ()
t.up ()
t.goto (-950, -450)
t.down ()
t.goto(-950,510)
t.goto(670,510)
t.goto(670,-1)
t.goto(-498.5,-1)
t.goto(-950,-450)
t.end_fill ()
t.color ("black")
t.goto(-950,510)
t.goto(670,510)
t.goto(670,-1)
t.goto(-498.5,-1)
t.goto(-950,-450)
t.up()
t.goto(-498.5,-1)
t.down()
t.setheading(90)
t.forward(500)
t.up()
t.goto(-415,60)
t.down()
t.setheading(0)
t.color("#000c1a")
t.begin_fill()
for x in range(2):
t.forward(1000)
t.left(90)
t.forward(290)
t.left(90)
t.end_fill()
t.up()
t.goto(-415,60)
t.down()
t.setheading(0)
t.color("black")
t.width(5)
for x in range(2):
t.forward(1000)
t.left(90)
t.forward(290)
t.left(90)
t.up()
t.goto(85,60)
t.down()
t.setheading(90)
t.forward(290)
def himmel1():
s.up()
s.goto(random.randint(-400,570),random.randint(100,360))
s.down()
s.dot(5, "#DFE6FF")
def himmel2():
s.up()
s.goto(random.randint(-400,570),random.randint(100,350))
s.down()
s.dot(5, "#757196")
def himmel3():
s.up()
s.goto(random.randint(-400,570),random.randint(100,350))
s.down()
s.dot(5, "#CED5FF")
def himmel4():
s.up()
s.goto(random.randint(-400,570),random.randint(100,350))
s.down()
def stern():
links = 144
vorne = 12
s.color("yellow")
s.begin_fill()
for x in range(1):
s.begin_fill()
s.up()
s.goto(random.randint(-400,570),random.randint(100,350))
s.down()
for x in range(5):
s.forward(vorne)
s.left(links)
s.end_fill()
for x in range(17):
himmel1()
himmel3()
himmel2()
himmel3()
for i in range(10):
stern()
This text is only here because the website tells me izs to much code and to less text so just ignore this.
Change all occurrences of -498.5 to -400. Then the wall and the floor will be aligned.
For such drawings, you can be easily confused by the coordinates of the points. It can get really complicated to debug and change the code. You might want to store the coordinates of the points in some variables such that you won't get lost. In your case, the coordinate point (-400, -1) is miswritten as (-498.5, -1) when you are drawing the wall.
I was just wondering how can I make a bar graph that uses the key-pair from a dictionary to use it for the x-axis and the key-value to draw the graph.
For example: dic = {'0-10': 24, '10-20': 20, '20-30': 22, '30-40': 27, '40-50': 150, '50-60': 0, '60-70': 231, '70-80': 467, '80-90': 443, '90-100': 86}
I want the '0-10' as the x-axis and the number as the height of the bar. Here's my my code so bar but I can't find a find to draw the x-axis for it. Also the y-axis will always be 100.
import turtle
from main import value_pairs, key_values
# Basic function to draw "bar graph", it takes the height as prameter
def drawBar(t, height):
t.begin_fill()
t.left(90)
t.forward(height)
t.write(str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.left(90)
t.end_fill()
xs = value_pairs
maxheight = max(xs)
numbars = len(xs)
border = 5
wn = turtle.Screen()
wn.setworldcoordinates(0-border, 0-border, 40*numbars+border, maxheight+border)
wn.bgcolor("white")
t = turtle.Turtle()
t.color("black")
t.fillcolor("white")
t.pensize(3)
for x in value_pairs:
drawBar(t, x)
I've reworked your attempt to roughly do what you describe, filling in missing pieces from your posted code:
from turtle import Screen, Turtle
FONT_HEIGHT = 18
FONT = ('Arial', FONT_HEIGHT, 'normal')
BORDER = FONT_HEIGHT
def drawBar(t, datum):
label, height = datum
t.left(90)
t.begin_fill()
t.forward(height)
t.right(90)
t.forward(20)
if height > FONT_HEIGHT:
t.write(height, align="center", font=FONT)
t.forward(20)
t.right(90)
t.forward(height)
t.end_fill()
t.left(90)
t.backward(40)
t.forward(20)
t.write(label, align="center", font=FONT)
t.forward(20)
data = {
'0-10': 24,
'10-20': 20,
'20-30': 22,
'30-40': 27,
'40-50': 150,
'50-60': 0,
'60-70': 231,
'70-80': 467,
'80-90': 443,
'90-100': 86
}
maxheight = max(data.values())
numbars = len(data)
screen = Screen()
screen.setworldcoordinates(-BORDER, -BORDER, 40 * numbars + BORDER, maxheight + BORDER)
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
turtle.fillcolor('white')
turtle.pensize(3)
for datum in data.items():
drawBar(turtle, datum)
turtle.hideturtle()
screen.exitonclick()
Matplotlib is the gold standard for data visualization with Python, but it's always fun to try these things with turtle to get a better understanding of what's involved in drawing plots.
Warning: this approach assumes ordered dictionaries -- not a historically safe assumption.
The fillcolor() is not working at all in this function -- I can not figure out why. It worked in all of my other functions:
from turtle import Turtle
from random import randint
t = Turtle()
def rocks():
for i in range(5):
t.penup()
t.goto(randint(-300,0), randint(-200,0))
for x in range(40):
t.pendown()
t.fillcolor("gray")
t.fillcolor()
t.begin_fill()
t.forward(5)
t.left(25)
t.right(27)
t.forward(5)
t.right(20)
t.end_fill()
t.speed("fastest")
rocks()
The problem is that you have the begin_fill() & end_fill() inside your loop which means they are trying to fill short line segments. You need them around your loop to fill the entire shape:
from turtle import Turtle, Screen
from random import randint
def rocks(t):
t.fillcolor('gray')
for _ in range(5):
t.penup()
t.goto(randint(-300, 0), randint(-200, 0))
t.begin_fill()
for _ in range(15):
t.pendown()
t.forward(5)
t.right(2)
t.forward(5)
t.right(22)
t.end_fill()
screen = Screen()
turtle = Turtle()
turtle.speed('fastest')
rocks(turtle)
turtle.hideturtle()
screen.exitonclick()
I can't figure out how to calculate the coordinates for the dots inside the circle. Right now my program is supposed to calculate the dots inside the circle within the square and divide that by the total amount of dots.
import turtle
import random
t=turtle.Turtle()
insideCircleCount = 0
def square():
for y in range(4):
t.forward(200)
t.left(90)
def circle():
t.penup()
t.setpos(100,0)
t.pendown()
t.circle(100)
def randomDot():
z=int(input("iterations:"))
for y in range(z):
t.penup()
t.pensize(1)
x=random.randint(0,200)
y=random.randint(0,200)
t.goto(x,y)
t.pendown()
t.dot()
insideCircle(x,y)
def insideCircle(x,y):
if ((x*x + y*y)<100):
insideCircleCount+=1
#main
square()
circle()
randomDot()
print('Dots inside circle account for ', insideCircleCount)
import turtle
import random
t=turtle.Turtle()
insideCircleCount = 0
def square():
for y in range(4):
t.forward(200)
t.left(90)
def circle():
t.penup()
t.setpos(100,0)
t.pendown()
t.circle(100)
def randomDot():
z=int(input("iterations:"))
for y in range(z):
t.penup()
t.pensize(1)
x=random.randint(0,200)
y=random.randint(0,200)
t.goto(x,y)
t.pendown()
t.dot()
insideCircle(x,y)
def insideCircle(x,y):
# Here the circle has a offset with center at (100,100)
#(x – h)^2 + (y – k)^2 = r2
if (((x-100)**2 + (y-100)**2) < 100**2):
# you were trying to access a local variable before assignment
#this fixes that
global insideCircleCount
insideCircleCount+=1
#main
square()
circle()
randomDot()
print('Dots inside circle account for ', insideCircleCount)
turtle.mainloop() #stops the window from closing
I am drawing the Indian flag using turtle and python. I have got the rectangles and colours down so far, but am struggling to make the chakra in the middle.
It has 24 spokes, and is surrounded by a closed circle. Any tips on how to accomplish this?
This is my code now:
import turtle
def drawRectangle (t, w, h, c):
t.fillcolor(c)
t.begin_fill()
for i in range(2):
t.forward(h)
t.left(90)
t.forward(w)
t.left(90)
t.end_fill()
def main ():
wn = turtle.Screen()
chloe = turtle.Turtle()
drawRectangle(chloe,50,200, "chartreuse3")
chloe.up()
chloe.goto(0,-100)
chloe.down()
drawRectangle(chloe,50,200, "orange1")
chloe.up()
chloe.goto(100,-25)
chloe.down()
chloe.pencolor("blue4")
for i in range(24):
chloe.forward(20)
chloe.backward(20)
chloe.left(15)
chloe.up()
chloe.goto(300,300)
main()
You can use the circle to draw the circle:
chloe.pencolor("blue4")
# draw the spokes
for i in range(24):
chloe.forward(20)
chloe.backward(20)
chloe.left(15)
# raise pen
chloe.up()
# head down
chloe.setheading(270)
# go forward 20
chloe.forward(20)
# reset heading
chloe.setheading(0)
# pen down
chloe.down()
# draw the circle
chloe.circle(20)
import turtle
def drawRectangle (t, l, b, c):
t.fillcolor(c)
t.begin_fill()
for i in range(2):
t.forward(b)
t.left(90)
t.forward(l)
t.left(90)
t.end_fill()
def main ():
wn = turtle.Screen()
india = turtle.Turtle()
drawRectangle(india,50,200, "orange1")
india.up()
india.goto(0,-100)
india.down()
drawRectangle(india,50,200, "green")
india.up()
india.goto(100,-20)
india.down()
india.pencolor("blue3")
for i in range(24):
india.forward(20)
india.backward(20)
india.left(15)
india.up()
india.goto(400,400)
main()