当前位置: 首页> 文旅> 文化 > 使用python绘制一个五颜六色的爱心

使用python绘制一个五颜六色的爱心

时间:2025/7/10 20:18:59来源:https://blog.csdn.net/summerriver1/article/details/139186049 浏览次数:0次

使用python绘制一个五颜六色的爱心

  • 介绍
  • 效果
  • 代码

介绍

使用numpy与matplotlib绘制一个七彩爱心!

效果

在这里插入图片描述

代码

import numpy as np
import matplotlib.pyplot as plt# Heart shape function
def heart_shape(t):x = 16 * np.sin(t)**3y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)return x, y# Create a figure and axis
fig, ax = plt.subplots()# Generate values for t
t = np.linspace(0, 2 * np.pi, 1000)# Generate heart shape coordinates
x, y = heart_shape(t)# Create a scatter plot with gradient colors
colors = plt.cm.rainbow(np.linspace(0, 1, len(t)))
for i in range(len(t) - 1):ax.plot(x[i:i+2], y[i:i+2], color=colors[i], linewidth=2)# Remove the axes
ax.axis('off')# Set the aspect of the plot to be equal
ax.set_aspect('equal')# Show the plot
plt.show()
关键字:使用python绘制一个五颜六色的爱心

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: