失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 教你用python画不同类型的心形图虏获芳心 值得收藏!!

教你用python画不同类型的心形图虏获芳心 值得收藏!!

时间:2018-11-23 10:38:26

相关推荐

教你用python画不同类型的心形图虏获芳心 值得收藏!!

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

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

import numpy as npimport matplotlib.pyplot as pltT = 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()

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

import turtleturtle.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()

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

import matplotlib.pyplot as pltfrom matplotlib import animationimport numpy as npimport matht = 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

alpha=0.3

alpha=0.6

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

# coding=utf-8# 3D心形import matplotlib.pyplot as pltimport numpy as npimport matplotlibmatplotlib.rcParams['axes.unicode_minus'] = Falsedef 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**3def plot_implicit(fn, bbox=(-1.5, 1.5)):xmin, xmax, ymin, ymax, zmin, zmax = bbox*3fig = 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, A2Z = fn(X, Y, z)cset = ax.contour(X, Y, Z+z, [z], zdir='z', colors=('r',))for y in B:# 在XZ平面绘制等高线X, Z = A1, A2Y = fn(X, y, Z)cset = ax.contour(X, Y+y, Z, [y], zdir='y', colors=('red',))for x in B:# 在YZ平面绘制等高线Y, Z = A1, A2X = 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)

利用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)]))

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

import timeILY = 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);

完美表白工具

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

__EOF__

本文作者:奈何缘浅本文链接:/nhyq-wyj/p/14940068.html

如果觉得《教你用python画不同类型的心形图虏获芳心 值得收藏!!》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。