当前位置:   article > 正文

520来了想要表白的看这里,教你用python画不同类型的心形图虏获芳心,值得收藏!!_python心形图案

python心形图案

在这里插入图片描述
python中有很多方法去画心形图用来表白,其中最典型的就是数学中心型曲线:r=a(1-sinθ),下面就总结以下python中那些画心形图的方法。末尾有一个完美的表白工具,可以直接使用。


点击免费领取《CSDN大礼包》:

最新全套【Python入门到进阶资料 & 实战源码 &安装工具】


数学中美丽的心形线:r=a(1-sinθ)

import numpy as np
import matplotlib.pyplot as plt
T = np.linspace(0, 2 * np.pi, 1024)  # 角度范围 0-2*pi,划为1024等份
plt.axes(polar=True)    # 开启极坐标模式
plt.plot(T, 1. - np.sin(T), color="r")
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述

1、利用python的turtle教你动态的爱心图表白

import turtle
turtle.color('red', 'pink')
turtle.pensize(2)
turtle.pendown()
turtle.setheading(150)
turtle.begin_fill()
turtle.fd(50)
turtle.circle(50 * -3.745, 45)
turtle.circle(50 * -1.431, 165)
turtle.left(120)
turtle.circle(50 * -1.431, 165)
turtle.circle(50 * -3.745, 45)
turtle.fd(50)
turtle.end_fill()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在这里插入图片描述

2、利用python的plt教你画渐变颜色爱心图表白

import matplotlib.pyplot as plt
from matplotlib import animation
import numpy as np
import math

t = np.linspace(0, math.pi, 1000)
x = np.sin(t)
y = np.cos(t) + np.power(x, 2.0 / 3)  # 心型曲线的参数方程

plt.scatter(x, y, c=y, cmap=plt.cm.Reds, edgecolor='none', s=40)
plt.scatter(-x, y, c=y, cmap=plt.cm.Reds, edgecolor='none', s=40)  # 渐变颜色曲线
# 填充曲线
plt.fill(x, y, 'r', alpha=0.6)
plt.fill(-x, y, 'r', alpha=0.6)

plt.axis([-2, 2, -2, 2])  # 坐标轴范围
plt.title("I love you", fontsize=30)
# 取消坐标轴显示
plt.axis('off')
# 保存文件
plt.savefig("❤图1.png")  # 在 plt.show() 之前调用 plt.savefig()
plt.show()
alpha=0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

在这里插入图片描述

alpha=0.3
  • 1

在这里插入图片描述

alpha=0.6
  • 1

在这里插入图片描述

3、利用python的plt教你画3D爱心图表白

# coding=utf-8
# 3D心形

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

matplotlib.rcParams['axes.unicode_minus'] = False


def heart_3d(x, y, z):
    return (x**2+(9/4)*y**2+z**2-1)**3-x**2*z**3-(9/80)*y**2*z**3


def plot_implicit(fn, bbox=(-1.5, 1.5)):
    xmin, xmax, ymin, ymax, zmin, zmax = bbox*3
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    A = np.linspace(xmin, xmax, 100)    # 轮廓分辨率
    B = np.linspace(xmin, xmax, 40)     # 切片数量
    A1, A2 = np.meshgrid(A, A)          # 绘制等高线的网格

    for z in B:     # 在XY平面绘制等高线
        X, Y = A1, A2
        Z = fn(X, Y, z)
        cset = ax.contour(X, Y, Z+z, [z], zdir='z', colors=('r',))

    for y in B:     # 在XZ平面绘制等高线
        X, Z = A1, A2
        Y = fn(X, y, Z)
        cset = ax.contour(X, Y+y, Z, [y], zdir='y', colors=('red',))

    for x in B:     # 在YZ平面绘制等高线
        Y, Z = A1, A2
        X = fn(x, Y, Z)
        cset = ax.contour(X+x, Y, Z, [x], zdir='x',colors=('red',))

    ax.set_zlim3d(zmin, zmax)
    ax.set_xlim3d(xmin, xmax)
    ax.set_ylim3d(ymin, ymax)
    # 标题
    plt.title("I love you", fontsize=30)
    # 取消坐标轴显示
    plt.axis('off')
    # 保存文件
    plt.savefig("3D_❤图.png")  # 在 plt.show() 之前调用 plt.savefig()
    plt.show()


if __name__ == '__main__':
    plot_implicit(heart_3d)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

在这里插入图片描述

4、利用python一行代码教你画爱心图表白

print('\n'.join([''.join([('ILOVEYOUWP'[(x-y) % 10]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else' ')for x in range(-60,60)])for y in range(30,-30,-1)]))
  • 1

在这里插入图片描述

5、利用python几行代码教你画爱心图表白

import time
ILY = input('请输入你想对她说的话:')
for item in ILY.split():
    print('\n'.join([''.join([(item[(x-y) % len(item)] if ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3 <= 0 else ' ') for x in range(-60, 60)]) for y in range(30, -30, -1)]))
    time.sleep(3);
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6、完美表白工具

import turtle
import time


# 清屏函数
def clear_all():
    turtle.penup()
    turtle.goto(0, 0)
    turtle.color('white')
    turtle.pensize(800)
    turtle.pendown()
    turtle.setheading(0)
    turtle.fd(300)
    turtle.bk(600)


# 重定位海龟的位置
def go_to(x, y, state):
    turtle.pendown() if state else turtle.penup()
    turtle.goto(x, y)


# 画爱心
def draw_heart(size):
    turtle.color('red', 'pink')
    turtle.pensize(2)
    turtle.pendown()
    turtle.setheading(150)
    turtle.begin_fill()
    turtle.fd(size)
    turtle.circle(size * -3.745, 45)
    turtle.circle(size * -1.431, 165)
    turtle.left(120)
    turtle.circle(size * -1.431, 165)
    turtle.circle(size * -3.745, 45)
    turtle.fd(size)
    turtle.end_fill()


# 画出发射爱心的小人
def draw_people(x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.pensize(2)
    turtle.color('black')
    turtle.setheading(0)
    turtle.circle(60, 360)
    turtle.penup()
    turtle.setheading(90)
    turtle.fd(75)
    turtle.setheading(180)
    turtle.fd(20)
    turtle.pensize(4)
    turtle.pendown()
    turtle.circle(2, 360)
    turtle.setheading(0)
    turtle.penup()
    turtle.fd(40)
    turtle.pensize(4)
    turtle.pendown()
    turtle.circle(-2, 360)
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(-90)
    turtle.pendown()
    turtle.fd(20)
    turtle.setheading(0)
    turtle.fd(35)
    turtle.setheading(60)
    turtle.fd(10)
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(-90)
    turtle.pendown()
    turtle.fd(40)
    turtle.setheading(0)
    turtle.fd(35)
    turtle.setheading(-60)
    turtle.fd(10)
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(-90)
    turtle.pendown()
    turtle.fd(60)
    turtle.setheading(-135)
    turtle.fd(60)
    turtle.bk(60)
    turtle.setheading(-45)
    turtle.fd(30)
    turtle.setheading(-135)
    turtle.fd(35)
    turtle.penup()


# 第一个画面,显示文字
def page0():
    turtle.penup()
    turtle.goto(-350, 0)
    turtle.color('red')
    turtle.write('有你生活成缤纷多彩', font=('宋体', 60, 'normal'))
    turtle.penup()
    turtle.goto(-160, -180)
    draw_heart(30)
    turtle.penup()
    turtle.goto(0, -180)
    draw_heart(30)
    turtle.penup()
    turtle.goto(160, -180)
    draw_heart(30)
    time.sleep(3)


# 第二个画面,显示发射爱心的小人
def page1():
    turtle.speed(10)
    turtle.penup()
    turtle.goto(-200, -200)
    turtle.color('red')
    turtle.pendown()
    turtle.write('WYJ       WP', font=('wisdom', 50, 'normal'))
    turtle.penup()
    turtle.goto(0, -180)
    draw_heart(10)
    draw_people(-250, 20)
    turtle.penup()
    turtle.goto(-150, -30)
    draw_heart(14)
    turtle.penup()
    turtle.goto(-20, -60)
    draw_heart(25)
    turtle.penup()
    turtle.goto(250, -100)
    draw_heart(45)
    turtle.hideturtle()
    # 写送给谁
    turtle.pencolor("PINK")
    turtle.penup()
    turtle.goto(300, 200)
    turtle.write(str, move=False, align='center', font=("方正舒体", 30, 'normal'))
    time.sleep(3)


def main():
    turtle.setup(900, 500)
    page0()
    clear_all()
    page1()
    clear_all()
    turtle.done()


if __name__ == '__main__':
     str = input('请输入表白语:')
    main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

心形代码就分享到这里,完整的源码已经打包好了,需要的朋友可以扫描下方二维码免费自取!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/959700
推荐阅读
相关标签
  

闽ICP备14008679号