python 实验9

1、绘制一个填充的正方体。
分析:从视角上看正方体一般只能看到三个而,正立而、顶面和右侧面。因此,只需要对这三个面(分别为填充红色、绿色和蓝色)进行绘制和填充即可。

import turtle # 导入模块

# 画正方形正面
n = 100        # 正方形边长
turtle.penup()
turtle.goto(-100,-50)
turtle.pendown()
turtle.pencolor('red')        # 画笔颜色为红色
turtle.begin_fill()
turtle.fillcolor('red')            # 填充颜色为红色
for i in range(4):
  turtle.forward(n)
  turtle.left(90)
turtle.end_fill()
#画正方形顶面
turtle.penup()
turtle.goto(-100,n-50)
turtle.pendown()
turtle.pencolor('green')            #画笔颜色为绿色
turtle.begin_fill()
turtle.fillcolor('green')             #填充颜色为绿色
turtle.left(45)
turtle.forward(int(n * 0.6))                 #倾斜边长为60
turtle.right(45)
turtle.forward(n)
turtle.left(360 - 135)
turtle.forward(int(n * 0.6))       #倾斜边长为60
turtle.end_fill()
# 画正方形右侧面
turtle.left(45)
turtle.penup()
turtle.goto(n-100,-50)
turtle.pendown()
turtle.pencolor('blue')                   #画笔颜色为蓝色
turtle.begin_fill()
turtle.fillcolor('blue')           #填充颜色为蓝色
turtle.left(135)
turtle.forward(int(n * 0.6))                    #倾斜边长为60
turtle.left(45)
turtle.forward(n)
turtle.left(135)
turtle.forward(int(n * 0.6))             # 倾斜边长为60
turtle.right(90)
turtle.end_fill()
turtle.done()

2、使用Turtle模块绘制一个五角星。

import turtle
turtle.penup()
turtle.goto(-10, 0)
turtle.pendown()
turtle.color('red')
turtle.pencolor('red')
for i in range(5):
    turtle.forward(70)
    turtle.left(72)
    turtle.forward(70)
    turtle.right(144)
turtle.hideturtle()
turtle.done()

3、使用Random模块和NumPy库生成一个3行4列的多维数组,数组中的每个元素为1~100之间的随机整数,然后求该数组所有元素的平均值。

import random
import numpy as np
arr =np.array([np.random.randint(1, 101, 4), np.random.randint(1, 101, 4), np.random.randint(1, 101, 4)])
print("数组元素是1~100之间的随机整数的三行四列的二维数组:")
print(arr)
print("该数组的平均值为:%.4f"%(np.sum(arr)/12))

4、使用 Matplotlib库绘制y =2x+1和y=x2的图形,并设置坐标轴的名称和图例。

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-10, 10, 1)
plt.xlable('x轴', fontproperties = 'SimHei' , fontsize=16)
plt.ylable('y轴', fontproperties = 'SimHei' , fontsize=16)
plt.plot(x, 2*x+1, 'red', lw=2)
plt.plot(x, x**2, 'blue', lw=2)
plt.legend(['2*x+1', 'x**2'])
plt.stow()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值