Teach you to express love with Python

Although 2.14 Valentine's Day has passed, there will be White Valentine's Day, Black Valentine's Day, Rose Valentine's Day...

If you have someone in mind, hurry up and seize the opportunity to express your love!

Python romantic confession!

Python has a super powerful drawing function, which can give you a helping hand when you are ready to confess your love.

Python import package

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

send a rose

fig = plt.figure(figsize=(12, 10))
ax = fig.gca(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')
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()

0935fe1933755d78025f68d720cd9270.png

What! The color is too monotonous? How about a colorful one?

fig = plt.figure(figsize=(12, 10))
ax = fig.gca(projection='3d')

[x, t] = np.meshgrid(np.array(range(25)) / 24.0, 
                     np.arange(0, 575.5, 0.5) / 575 * 17 * np.pi - 2 * np.pi)
p = (np.pi / 2) * np.exp(-t / (8 * np.pi))

u = 1 - (1 - np.mod(3.6 * t, 2 * np.pi) / np.pi) ** 4 / 2
y = 2 * (x ** 2 - x) ** 2 * np.sin(p)
r = u * (x * np.sin(p) + y * np.cos(p))
h = u * (x * np.cos(p) - y * np.sin(p))

c = cm.gist_rainbow_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()

dddf30ad15c2a8589daff091ba010017.png

Get rid of the green and keep the red of the rose

fig = plt.figure(figsize=(12, 10))
ax = fig.gca(projection='3d')

[x, t] = np.meshgrid(np.array(range(25)) / 24.0, 
                     np.arange(0, 575.5, 0.5) / 575 * 17 * np.pi - 2 * np.pi)

p = (np.pi / 2) * np.exp(-t / (8 * np.pi))
u = 1 - (1 - np.mod(3.6 * t, 2 * np.pi) / np.pi) ** 4 / 2
y = 2 * (x ** 2 - x) ** 2 * np.sin(p)
r = u * (x * np.sin(p) + y * np.cos(p))
h = u * (x * np.cos(p) - y * np.sin(p))
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()

8b6daf315c4896c4337bd59e0babc926.png

Roses are in full bloom! I wish you a successful confession!

fig = plt.figure(figsize=(12, 10))
ax = fig.gca(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')

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

4c9309a44d77817bfd95b3840a8e03b8.png

Drawing diagrams with Python turtles

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(10000)
    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")
            return

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

15383bcf3b267f419c91d7426a587446.png

Above we used Matplotlib drawing and Python drawing

 
  
最后推荐一下自己的专栏
一个人学习GPT费时费力,而且遇到问题没有人可以商量,交流和指导,浪费了大量的时间和精力。现在当务之急是要迅速麻利快使用这个超强AI,帮助你提高工作学习的效率,节省成本!
原价299,目前早鸟的价格是109元(内容永久有效),今晚会调整价格!满200涨20,一直涨到原价。目前很多星球都是动辄几百的价格。微信联系小编:coder_v5
目前手上还有少量的内含5美金的GPT账号(可修改密码),大家知道注册越来越困难。这个账号市场价格已经炒到了40元一个。报名我们专栏的同学我们会送这个独立账号!


内容会持续推出,解锁更多高级好玩的技巧,感兴趣的火速扫码加入吧!
往期推荐:
通过ChatGPT进行副业赚钱的5个技巧!
玩的停不下来!!用Python+ChatGPT打造超强的微信机器人!
ChatGPT4已经来了,30秒做一个弹球游戏!
靠ChatGPT赚了3000块!
推荐阅读:
入门: 最全的零基础学Python的问题  | 零基础学了8个月的Python  | 实战项目 |学Python就是这条捷径
干货:爬取豆瓣短评,电影《后来的我们》 | 38年NBA最佳球员分析 |   从万众期待到口碑扑街!唐探3令人失望  | 笑看新倚天屠龙记 | 灯谜答题王 |用Python做个海量小姐姐素描图 |碟中谍这么火,我用机器学习做个迷你推荐系统电影
趣味:弹球游戏  | 九宫格  | 漂亮的花 | 两百行Python《天天酷跑》游戏!
AI: 会做诗的机器人 | 给图片上色 | 预测收入 | 碟中谍这么火,我用机器学习做个迷你推荐系统电影
小工具: Pdf转Word,轻松搞定表格和水印! | 一键把html网页保存为pdf!|  再见PDF提取收费! | 用90行代码打造最强PDF转换器,word、PPT、excel、markdown、html一键转换 | 制作一款钉钉低价机票提示器! |60行代码做了一个语音壁纸切换器天天看小姐姐!|

Guess you like

Origin blog.csdn.net/cainiao_python/article/details/130120169