matplotlib给女朋友画一个爱心吧,这份满满的爱意,一定要记得收下

2021-04-22 10:16:07 浏览数 (1)

使用matplotlib给女朋友画一个爱心吧,这份满满的爱意,一定要记得收下

1、起因

女朋友总是查你的电脑,于是想到是否可以画一个爱心呢!最后经过不懈的努力找到了一个非常简单的实现方式,用起来看一下吧!

2、代码实现

普通版:

代码语言:javascript复制
import matplotlib
import matplotlib.path as mpath
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from pylab import mpl

mpl.rcParams['font.sans-serif'] =["SimHei"]#解决乱码问题
mpl.rcParams['axes.unicode_minus'] = False
matplotlib.rcParams['toolbar'] = 'None'#去除工具栏
fig, ax = plt.subplots()
Path = mpath.Path
path_data,list= [
    (Path.MOVETO, (1,-3)),    (Path.LINETO, (-1, -1)),
    (Path.CURVE4, (-2,1)),    (Path.CURVE4, (0, 3)),
    (Path.LINETO, (1, 1)),    (Path.CURVE4, (2, 3)),
    (Path.CURVE4, (4, 1)),    (Path.CURVE4, (3, -1)),
    (Path.CLOSEPOLY, (1,-3))],[]
def love(path_data):
    list.append(path_data)
    ax.clear()#每次绘画之前,先清除,不清除就会重叠在一起
    codes, verts = zip(*list)
    path = mpath.Path(verts, codes)
    patch = mpatches.PathPatch(path, facecolor='r', alpha=0.5)
    ax.add_patch(patch)
    plt.box(False)  # 去除坐标轴
    plt.axis('off')  # 去除坐标轴数据
    plt.xlim(-2,4)#设置x轴坐标范围
    plt.ylim(-3, 3)#设置y轴坐标范围
    if len(list)==9:
        plt.text(1,0, '喜欢的人',ha='center',va='baseline')#添加文字
def data():#迭代器
    for i in range(0, 9):
        yield path_data[i]
animator=animation.FuncAnimation(fig, love, frames=data())
plt.show()

特殊版:

代码语言:javascript复制
import matplotlib
import matplotlib.path as mpath
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from pylab import mpl

mpl.rcParams['font.sans-serif'] =["SimHei"]#解决乱码问题
mpl.rcParams['axes.unicode_minus'] = False
matplotlib.rcParams['toolbar'] = 'None'#去除工具栏
fig, ax = plt.subplots()
Path = mpath.Path
path_data,list= [
    (Path.MOVETO, (1,-3)),    (Path.LINETO, (-1, -1)),
    (Path.CURVE4, (-2,1)),    (Path.CURVE4, (0, 3)),
    (Path.LINETO, (1, 1)),    (Path.CURVE4, (2, 3)),
    (Path.CURVE4, (4, 1)),    (Path.CURVE4, (3, -1)),
    (Path.CLOSEPOLY, (1,-3))],[]
def love(path_data):
    list.append(path_data)
    ax.clear()#每次绘画之前,先清除,不清除就会重叠在一起
    codes, verts = zip(*list)
    path = mpath.Path(verts, codes)
    patch = mpatches.PathPatch(path, facecolor='r', alpha=0.5)
    ax.add_patch(patch)
    plt.box(False)  # 去除坐标轴
    plt.axis('off')  # 去除坐标轴数据
    plt.xlim(-2,4)#设置x轴坐标范围
    plt.ylim(-3, 3)#设置y轴坐标范围
    if len(list)==9:
        plt.text(1,0, '喜欢的人',ha='center',va='baseline')#添加文字
def data():#迭代器
    for i in range(0, 9):
        yield path_data[i]
animator=animation.FuncAnimation(fig, love, frames=data())
plt.get_current_fig_manager().full_screen_toggle()#最大化窗口,无法关闭
plt.show()

有人会问,这两个版本有什么不一样了,不都是一样的吗?

这里统一解答一下,特殊版又名作死版。当你运行起来之后你就会发现。。。。没有关闭窗口操作,想退出,退出不了。想打开任务管理器,底部菜单都没有。

怎么办,只能重启。当然这是小白的操作,你可以 ctrl D最小化,然后就可以关闭了。

0 人点赞