如何在Python中绘制圆弧(圆的一部分)

3
我希望有360个PNG位图,每个位图是一个弧形,表示进度中的一步。以下位图表示60度(距离顶部60度)和120度的步骤。

Step 60 Step 120

如何在代码中绘制这些位图?
编辑:我现在可以绘制它们,但不知道如何将起始点设置在顶部而不是底部。
import turtle
wn = turtle.Screen
turtle.hideturtle()
turtle.hideturtle()
turtle.ht()
turtle.speed(0)

turtle.pensize(11)
turtle.color("grey")
turtle.circle(200)

turtle.color("red")
turtle.circle(200, 60, 3600)

cv = turtle.getcanvas()
cv.postscript(file="circle.ps", colormode='color')

turtle.done()

1
抱歉,这不是 Stack Overflow 的工作方式。 - Mad Physicist
由于您的问题标记为“python”,因此在绘图方面,matplotlib 经常用于 python。也许您想要搜索一下相关信息。 - ZisIsNotZis
2个回答

3

一些简单的绘制弧线的代码:

import matplotlib.pyplot as plt
from matplotlib.patches import Arc
plt.figure(figsize=(6, 6)) # set image size
plt.subplots_adjust(0, 0, 1, 1) # set white border size
ax = plt.subplot()
for i in range(1, 361):
    plt.cla() # clear what's drawn last time
    ax.invert_xaxis() # invert direction of x-axis since arc can only be drawn anti-clockwise
    ax.add_patch(Arc((.5, .5), .5, .5, -270, theta2=i, linewidth=5, color='red')) # draw arc
    plt.axis('off') # hide number axis
    plt.savefig(str(i)+'.png', facecolor='black') # save what's currently drawn

你可能需要添加一些代码来实现图片的效果。以下是结果截图链接:enter image description here

2

使用这个答案作为指南,首先我们需要一个程序来绘制并转储图像:

from turtle import Screen, Turtle

def save(counter=[1]):  # dangerous default value
    screen.getcanvas().postscript(file="arc{0:03d}.eps".format(counter[0]))
    counter[0] += 1

screen = Screen()
screen.setup(330, 330)
screen.colormode(255)

turtle = Turtle(visible=False)
turtle.speed('fastest')

turtle.penup()
turtle.goto(-150, -150)

turtle.begin_fill()
for _ in range(4):
    turtle.forward(300)
    turtle.left(90)
turtle.end_fill()

turtle.home()
turtle.width(10)
turtle.color(183, 0, 2)
turtle.sety(140)
turtle.pendown()

save()

for _ in range(360):
    turtle.circle(-140, 1)
    save()

我在第6步放弃了引用的答案,转而使用ezgif.com制作这个动态PNG图像:

enter image description here


非常感谢。我可以使用ImageMagick工具将“eps”文件转换为png: magick mogrify -format png -density 300 -colorspace sRGB -background transparent -units PixelsPerInch -resize 330x330 *.eps - Ngo Thanh Nhan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接