我正在尝试解决“思考Python”第5章的练习9。 (版本:http://openbookproject.net/thinkcs/python/english3e/conditionals.html)。它涉及从turtle.write中移动文本,因此当值为负时它不会与条形图重叠。我尝试过使用三重引号"""像这样"""为了在文本之前添加额外的行,但额外的行在错误的位置。请帮帮忙?
import turtle
wn=turtle.Screen()
wn.bgcolor("lightgreen")
wn.title("Barcharts FTW")
pen=turtle.Turtle()
pen.hideturtle()
pen.color("blue","red")
pen.pensize(2)
pen.penup()
pen.goto(-300,-100)
def draw_bar (t,height):
t.pendown()
t.begin_fill()
t.lt(90)
t.fd(height)
t.write(" " + str(height))
t.rt(90)
t.fd(40)
t.rt(90)
t.fd(height)
t.end_fill()
t.lt(90)
t.penup()
t.fd(10)
xs = [48, 117, 200, 240, -160, 260, 220]
for v in xs:
draw_bar(pen,v)
wn.mainloop()
答案 0 :(得分:1)
简单地移动乌龟在不同的地方写文字
即
t.penup()
if height < 0:
t.fd(-15)
t.write(" " + str(height))
if height < 0:
t.fd(15)
t.pendown()
完整代码
import turtle
# --- functions ---
def draw_bar(t, height):
t.pendown()
t.begin_fill()
t.lt(90)
t.fd(height)
t.penup()
if height < 0:
t.fd(-15)
t.write(" " + str(height))
if height < 0:
t.fd(15)
t.pendown()
t.rt(90)
t.fd(40)
t.rt(90)
t.fd(height)
t.end_fill()
t.lt(90)
t.penup()
t.fd(10)
# --- main ---
wn = turtle.Screen()
wn.bgcolor("lightgreen")
wn.title("Barcharts FTW")
pen = turtle.Turtle()
pen.hideturtle()
pen.color("blue","red")
pen.pensize(2)
pen.penup()
pen.goto(-300,-100)
xs = [48, -117, 200, 240, -160, 260, 220]
for v in xs:
draw_bar(pen, v)
wn.mainloop()
答案 1 :(得分:0)
@Laura,下面是使用标记而不是绘图来构建条形图的不同方法。它还具有一些其他功能,无论您是绘制还是标记,它们都可能对您有用:它根据数据本身计算窗口中图形的居中位置;它使用align="center"
的{{1}}功能将标签与条形对齐;它显式设置字体而不是使用几乎不可读的默认值:
turtle.write()
要研究的另一个功能是from turtle import Turtle, Screen
BAR_WIDTH = 40
BAR_SPACING = 10
FONTSIZE = 12
FONT = ('Arial', FONTSIZE, 'bold')
STAMP_UNIT = 20
xs = [48, -117, 200, 240, -160, 260, 220]
def draw_bar(t, height):
y_baseline = t.ycor()
t.turtlesize(abs(height) / STAMP_UNIT, BAR_WIDTH / STAMP_UNIT, 2) # size the bar
t.left(90)
t.forward(height / 2) # move to the center of the bar
t.right(90)
t.stamp()
t.left(90)
t.forward(height / 2 + (-3 * FONTSIZE / 2 if height < 0 else 0)) # adjust font position when negative
t.right(90)
t.write(str(height), align="center", font=FONT) # center text on bar
t.forward(BAR_WIDTH + BAR_SPACING) # move to the next bar center x-wise
t.sety(y_baseline) # return to our calculated baseline y-wise
wn = Screen()
wn.bgcolor("lightgreen")
wn.title("Barcharts FTW")
pen = Turtle(shape="square", visible=False)
pen.color("blue", "red")
pen.penup()
pen.goto(len(xs) * (BAR_SPACING + BAR_WIDTH) / -2, -max(xs) - min(xs)) # center graph based on data
for value in xs:
draw_bar(pen, value)
wn.exitonclick()
。虽然对于大多数应用程序来说它可能很笨拙,但我已经看到它在绘图问题时非常成功,因为它可以让你重新定义乌龟的坐标系以满足你的数据需求。
答案 2 :(得分:0)
请注意if height < 0
部分,它会检查条形高度是否为负值。在这种情况下,乌龟会向后移动(tur_pen.forward(-15)
),这样当它写入值时,文本不会与条形图重叠。
# Modified turtle pie chart II
# By Dave Zabel 3/29/2017 - 3/31/2017
# Import turtle and random modules
import turtle
import random
values = random.sample(range(-250, 250), 15) # Initiate a list of 15 random values
def draw_bar(tur_pen, height):
"""Get turtle to draw one bar of height"""
tur_pen.begin_fill() # Begins color fill
tur_pen.left(90) # Starts a bar whose height is a value from the variable "values"
tur_pen.forward(height)
tur_pen.penup() # Sequence checks for negative value so as to print value UNDER the line
if height < 0:
tur_pen.forward(-15)
tur_pen.write(' ' + str(height)) # Prints the value of the bar height
if height < 0:
tur_pen.forward(15)
tur_pen.pendown() # Continues to complete one bar
tur_pen.right(90)
tur_pen.forward(40)
tur_pen.right(90)
tur_pen.forward(height)
tur_pen.left(90)
tur_pen.end_fill() # Stops the fill process
tur_pen.penup() # Has the pen skip between bars
tur_pen.forward(10)
tur_pen.pendown()
bar_screen = turtle.Screen() # Sets the attributes of the screen
bar_screen.title('Bar Graph')
bar_screen.bgcolor('light green')
bar = turtle.Turtle() # Sets the attributes of the pen
bar.pensize(3)
bar.speed(5)
bar.penup()
bar.goto(-360, 0)
bar.pendown()
fill_color = '' # Changes the fill color by height
for v in values:
if v >= 200:
fill_color = 'green'
elif v >= 100 < 200:
fill_color = 'yellow'
elif v > 0 < 100:
fill_color = 'gray'
elif v < 0:
fill_color = 'red'
bar.color('blue', fill_color)
draw_bar(bar, v) # Starts the process
bar_screen.mainloop() # Holds the screen open until the user closes it