用python画一百个同心圆的代码?

Python0224

用python画一百个同心圆的代码?,第1张

import matplotlib.pyplot as plt

from matplotlib.patches import Circle

# 创建一个图形对象

fig = plt.figure()

# 循环绘制一百个同心圆

for i in range(100):

# 使用 Circle 类创建圆形,并指定半径和圆心坐标

circle = Circle(xy=(0, 0), radius=i+1)

# 使用 fig.add_subplot() 方法将圆形添加到图中

ax = fig.add_subplot(1, 1, 1)

ax.add_patch(circle)

# 调用 plt.show() 方法显示图形

plt.show()

###################################

# coding=utf-8

# !/usr/bin/env python

# __author__ = 'pipi'

# ctime 2014.10.11

# 绘制椭圆和圆形

###################################

from matplotlib.patches import Ellipse, Circle

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111)

ell1 = Ellipse(xy = (0.0, 0.0), width = 4, height = 8, angle = 30.0, facecolor= 'yellow', alpha=0.3)

cir1 = Circle(xy = (0.0, 0.0), radius=2, alpha=0.5)

ax.add_patch(ell1)

ax.add_patch(cir1)

x, y = 0, 0

ax.plot(x, y, 'ro')

plt.axis('scaled')

# ax.set_xlim(-4, 4)

# ax.set_ylim(-4, 4)

plt.axis('equal') #changes limits of x or y axis so that equal increments of x and y have the same length

plt.show()

你可以试试,谢谢。

可以使用Python的Turtle模块来实现这一功能。首先,需要导入Turtle模块,然后使用Turtle的circle()函数来画圆,可以设置圆的半径,从小到大依次画圆。以下是使用Python的Turtle模块画圆的示例代码:

import turtle

t = turtle.Turtle()

# 从半径为10的圆开始

radius = 10

while radius <100:

t.circle(radius)

radius += 10

turtle.done()