2017-01-02 1074 views
0

我想解決'Think Python'第5章的練習9(版本:http://openbookproject.net/thinkcs/python/english3e/conditionals.html)。它包括從turtle.write中移動文本,以便在值爲負時與條形圖不重疊。爲了在文本之前添加額外的行,我試圖使用三重引號「」「像這樣的」「」,但額外的行進入錯誤的地方。請幫忙?如何在Python中使用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() 

回答

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() 
0

@Laura,下面是一個不同的方法來使用構建條形圖衝壓代替繪製的。它還具有一些其他功能,無論您是繪製還是印記,它都可能對您有用:它會根據數據本身計算窗口中心的位置;它使用turtle.write()align="center"功能將標籤與條對齊;它明確地設置字體,而不是使用幾乎無法讀取默認:

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() 

enter image description here

另一個功能尋找到是turtle.setworldcoordinates()。雖然對於大多數應用程序來說它可能很笨拙,但我已經看到它在繪圖問題上非常成功,因爲它可以讓您重新定義烏龜的座標系以滿足您的數據需求。

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 
+0

每次運行時創建一個不同的條形圖,只是玩得開心! –

相關問題