一、爱人表白树
用Python海龟画图表白
话不多说,直接附上代码
# 用Python海龟画图表白
import turtle
import random# 输入你爱人的姓名:
my_love = "刘亦菲"def love(x, y):lv = turtle.Turtle()lv.hideturtle()lv.up()lv.goto(x, y) # 定位到(x,y)def curvemove(): # 画圆弧for i in range(20):lv.right(10)lv.forward(2)lv.color('red', 'pink')lv.speed(999)lv.pensize(1)# 开始画爱心lv.down()lv.begin_fill()lv.left(140)lv.forward(22)curvemove()lv.left(120)curvemove()lv.forward(22)lv.write(my_love, font=("Arial", 12, "normal"), align="center")lv.left(140)lv.end_fill()def tree(branchLen, t):if branchLen > 5:if branchLen < 20:t.color("blue")t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))t.down()t.forward(branchLen)love(t.xcor(), t.ycor())t.up()t.backward(branchLen)t.color("brown")returnt.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))t.down()t.forward(branchLen)# 递归ang = random.uniform(15, 45)t.right(ang)tree(branchLen - random.uniform(12, 16), t)t.left(2 * ang)tree(branchLen - random.uniform(12, 16), t)t.right(ang)t.up()t.backward(branchLen)
myWin = turtle.Screen()
t = turtle.Turtle()
t.hideturtle()
t.speed(100)
t.left(90)
t.up()
t.backward(200)
t.down()
t.color("brown")
t.pensize(32)
t.forward(60)
tree(100, t)
myWin.exitonclick()
二、matplotlib画玫瑰
深红色、彩虹色、玫瑰色以及绽放式…直接附图附代码
# 导入对应的库和包
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltfrom matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D# 深红色、彩虹色、玫瑰色的玫瑰🌹
fig = plt.figure(figsize=(12, 10))
ax = fig.add_subplot(projection = '3d')
[x, t] = np.meshgrid(np.array(range(25)) / 24.0, np.arange(0, 575.5, 0.5) / 575 * 30 * np.pi - 4*np.pi)
p = (np.pi / 2) * np.exp(-t / (8 * np.pi))change = np.sin(20*t)/50
u = 1 - (1 - np.mod(3.3 * t, 2 * np.pi) / np.pi) ** 4 / 2 + change
y = 2 * (x ** 2 - x) ** 2 * np.sin(p)
r = u * (x * np.sin(p) + y * np.cos(p)) * 1.5
h = u * (x * np.cos(p) - y * np.sin(p))
# 玫瑰样式,根据需要调整
# c = plt.get_cmap('magma') # 深红色
c = cm.gist_rainbow_r # 彩虹色
# c= cm.get_cmap('spring_r') # 玫瑰色surf = ax.plot_surface(r * np.cos(t), r * np.sin(t), h, rstride=1, cstride=1, cmap= c, linewidth=0, antialiased=True)
plt.show()
还有绽放式的玫瑰,图和代码如下:
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltfrom matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D# 玫瑰绽放🌹
fig = plt.figure(figsize=(12, 10))
ax = fig.add_subplot(projection = '3d')[x, t] = np.meshgrid(np.array(range(25)) / 24.0, np.arange(0, 575.5, 0.5) / 575 * 6 * np.pi - 4*np.pi)
p = (np.pi / 2) * np.exp(-t / (8 * np.pi))change = np.sin(10*t)/20
u = 1 - (1 - np.mod(5.2 * t, 2 * np.pi) / np.pi) ** 4 / 2 + change
y = 2 * (x ** 2 - x) ** 2 * np.sin(p)
r = u * (x * np.sin(p) + y * np.cos(p)) * 1.5
h = u * (x * np.cos(p) - y * np.sin(p))
# c= plt.get_cmap('spring_r')
# c = cm.gist_rainbow_r # 彩虹色
c = plt.get_cmap('magma') # 深红色
surf = ax.plot_surface(r * np.cos(t), r * np.sin(t), h, rstride=1, cstride=1, cmap= c, linewidth=0, antialiased=True)
plt.show()