python matplotlib 画心形线
2015-06-25 18:48
801 查看
心形线的数学表达式
水平方向: r=a(1-cosθ) 或 r=a(1+cosθ) (a>0)
垂直方向: r=a(1-sinθ) 或 r=a(1+sinθ) (a>0)
心形线的平面直角坐标系方程表达式分别为 x^2+y^2+a*x=a*sqrt(x^2+y^2) 和 x^2+y^2-a*x=a*sqrt(x^2+y^2)
x=a*(2*cos(t)-cos(2*t))
y=a*(2*sin(t)-sin(2*t))
所围面积为3/2*PI*a^2,形成的弧长为8a
直接生成心形线的python代码:
主要用到了matplotlib库来进行图像的绘制
用到的心形线方程是上图所示的方程,使用参数方程的形式来表示
动态生成心形线的python代码
动态生成,需要制作动画,主要用到了matplotlib中的animation库
极坐标方程
水平方向: r=a(1-cosθ) 或 r=a(1+cosθ) (a>0)垂直方向: r=a(1-sinθ) 或 r=a(1+sinθ) (a>0)
直角坐标方程
心形线的平面直角坐标系方程表达式分别为 x^2+y^2+a*x=a*sqrt(x^2+y^2) 和 x^2+y^2-a*x=a*sqrt(x^2+y^2)
参数方程
x=a*(2*cos(t)-cos(2*t))y=a*(2*sin(t)-sin(2*t))
所围面积为3/2*PI*a^2,形成的弧长为8a
直接生成心形线的python代码:
# coding:utf-8 __author__ = 'taohao' import matplotlib.pyplot as plt from matplotlib import animation import numpy as np import math def drawHeart(): t = np.linspace(0, math.pi, 1000) x = np.sin(t) y = np.cos(t) + np.power(x, 2.0/3) plt.plot(x, y, color='red', linewidth=2, label='h') plt.plot(-x, y, color='red', linewidth=2, label='-h') plt.xlabel('t') plt.ylabel('h') plt.ylim(-2, 2) plt.xlim(-2, 2) plt.legend() plt.show() drawHeart()
主要用到了matplotlib库来进行图像的绘制
用到的心形线方程是上图所示的方程,使用参数方程的形式来表示
动态生成心形线的python代码
# coding:utf-8 __author__ = 'taohao' import matplotlib.pyplot as plt from matplotlib import animation import numpy as np import math figure = plt.figure() axes = plt.axes(xlim=(-2, 2), ylim=(-2, 2)) line1, = axes.plot([], [], color='red', linewidth=2, label='1') line2, = axes.plot([], [], color='red', linewidth=2, label='2') def init(): line1.set_data([], []) line2.set_data([], []) return line1, line2 def animate(i): print i t = np.linspace(0, i/math.pi, 100) x = np.sin(t) y = np.cos(t) + np.power(x, 2.0/3) line1.set_data(x, y) line2.set_data(-x, y) return line1, line2 ani = animation.FuncAnimation(figure, animate, init_func=init, frames=14, interval=200) # ani.save('Heart.mp4') save as mp4 but need to install video-encoder. i did not install it, so this line makes exeception plt.show()
动态生成,需要制作动画,主要用到了matplotlib中的animation库
相关文章推荐
- 简明 Python 编程规范
- 如何用python发送邮件
- python-rrdtool
- Python - 杨辉三角
- Python 学习笔记12
- 【python】 入门 搭建环境
- 和学生探讨吉林大学python问题
- Python之dir()与__dict__的区别
- 百度站长工具的主动推送功能_python实现
- python with使用的一些例子
- how to use Decimal by default instead of float in python
- Python性能优化的20条建议
- python核心编程——python对象
- 20150625 python周边你需要学习的知识
- Python 多线程 鸡肋也很好吃
- python各种数据类型之间的转换
- Python中的WSGI
- python函数参数带星号*
- python正则表达式复习4
- Python可调用对象