相关工具库很多,都是科学计算的东西:numpy、scipy、pandas、matplotlib、sympy、mayavi2
SciPy官网的介绍
http://www.scipy.org/getting-started.html
- NumPy's array type augments the Python language with an efficient data structure useful for numerical work, e.g., manipulating matrices. NumPy also provides basic numerical routines, such as tools for finding eigenvectors.
- SciPy contains additional routines needed in scientific work: for example, routines for computing integrals numerically, solving differential equations, optimization, and sparse matrices.
- The matplotlib module produces high quality plots. With it you can turn your data or your models into figures for presentations or articles. No need to do the numerical work in one program, save the data, and plot it with another program.
简要的说:
numpy--定义了数值数组和矩阵类型和它们的基本运算的语言扩展
scipy--使用numpy来做高等数学、信号处理、优化、统计和许多其它科学任务的语言扩展
pandas--对numpy做了进一步封装
matplotlib--python的2D绘图库,提供了类MATLAB的API
mayavi2--python的3D绘图库
sympy---python符号计算库
对sympy的解释比较抽象,运行实例感受一下
>>> import math,numpy,sympy >>> math.sqrt(8) 2.8284271247461903 >>> math.sqrt(8) * math.sqrt(2) 4.000000000000001 >>> numpy.sqrt(8) 2.8284271247461903 >>> numpy.sqrt(8) * numpy.sqrt(2) 4.0000000000000009 >>> sympy.sqrt(8) 2*sqrt(2) >>> sympy.sqrt(8) * sympy.sqrt(2) 4
以下内容只需要安装numpy、matplotlib和sympy
Windows安装numpy比较麻烦,有时可能遇到各种错,
可以直接去 http://www.lfd.uci.edu/~gohlke/pythonlibs/
搜索numpy,下载对应的系统预编译版本
下载完成用如下类似命令安装即可
pip install "numpy-1.10.4+mkl-cp35-none-win_amd64.whl"
1. 心形的公式
http://www.mathematische-basteleien.de/heart.htm
http://mathworld.wolfram.com/HeartCurve.html
http://mathworld.wolfram.com/HeartSurface.html
2. 绘制2D心形
1) 通过matplotlib
import matplotlib.pyplot as plt import numpy as np # -pi ~ pi, split 256 t = np.linspace(-np.pi, np.pi, 256, endpoint=True) X = 16 * (np.sin(t))**3 Y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t) # plot at line1 column1 plt.subplot(121) plt.plot(X, Y) # -3 ~ 3, step 0.02 x = y = np.arange(-3.0, 3.0, 0.02) X, Y = np.meshgrid(x, y) Z = (X**2 + Y**2 - 1)**3 - X**2 * Y**3 # plot at line1 column2 plt.subplot(122) plt.contour(X, Y, Z) plt.colorbar() plt.show()
2) 通过sympy
画等式特别方便
http://docs.sympy.org/latest/modules/plotting.html
>>> from sympy import symbols,plot_implicit >>> x,y = symbols('x y') >>> plot_implicit((x**2 + y**2 - 1)**3 - x**2 * y**3)
3. 绘制3D图形
from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt import numpy as np 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 heart_3d_2(x,y,z): return (2 * x**2 + 2 * y**2 + z**2 - 1)**3 - 0.1 * x**2 * z**3 - y**2 * z**3 def plot_implicit(fn, bbox=(-1.5, 1.5)): ''' create a plot of an implicit function fn ...implicit function (plot where fn==0) bbox ..the x,y,and z limits of plotted interval''' xmin, xmax, ymin, ymax, zmin, zmax = bbox*3 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') A = np.linspace(xmin, xmax, 100) # resolution of the contour B = np.linspace(xmin, xmax, 40) # number of slices A1, A2 = np.meshgrid(A, A) # grid on which the contour is plotted for z in B: # plot contours in the XY plane X, Y = A1, A2 Z = fn(X, Y, z) cset = ax.contour(X, Y, Z+z, [z], zdir='z', colors=('r',)) # [z] defines the only level to plot # for this contour for this value of z for y in B: # plot contours in the XZ plane 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: # plot contours in the YZ plane Y, Z = A1, A2 X = fn(x, Y, Z) cset = ax.contour(X+x, Y, Z, [x], zdir='x',colors=('red',)) # must set plot limits because the contour will likely extend # way beyond the displayed level. Otherwise matplotlib extends the plot limits # to encompass all values in the contour. ax.set_zlim3d(zmin, zmax) ax.set_xlim3d(xmin, xmax) ax.set_ylim3d(ymin, ymax) plt.show() if __name__ == '__main__': plot_implicit(heart_3d)
4. 最后通过turtle动态画一个心形
from turtle import * def curvemove(): for i in range(100): right(2) forward(2) color('red','pink') begin_fill() left(140) forward(111.65) curvemove() left(120) curvemove() forward(111.65) end_fill() done()
refer
Plotting implicit equations in 3d
how to draw a heart with pylab
Is it possible to plot implicit equations using Matplotlib
Is it possible to plot implicit 3d equation using sympy
How can I draw a heart using Python
相关推荐
matplotlib的使用,各种常用的matplotlib的使用和参数配置,python绘制各种数学函数图,直方图,饼图,折线图,极坐标图,等等,非常实用
Python 票据利率变化 数据分析 matplotlib绘制动态折线图 生成gif动画 展示不同时间粒度变化情况 按7天 1个月 3个月 6个月 9个月 1年分别画图 numpy pandas Jupyter notebook 数据分析 数据挖掘
爱心源码 python matplotlib numpy 绘制心形 保存为png格式 np.linspace np.meshgrid技术
matplotlib是python中最著名的绘制图,它提供了一整套和matlab相似的绘图工具
Python中使用matplotlib绘制图表
主要介绍了Python实现在tkinter中使用matplotlib绘制图形的方法,结合实例形式分析了Python使用tkinter与matplotlib进行正弦曲线图形绘制的相关操作技巧,需要的朋友可以参考下
# Python使用matplotlib绘制动态正弦曲线图 保存为gif动图 1. 使用matplotlib绘制正弦曲线; 2. 逐帧前进形成动画,可在界面显示也可保存为gif动画。
最近需要将实验数据画图出来,由于使用python进行实验,自然使用到了matplotlib来作图。 下面的代码可以作为画图的模板代码,代码中有详细注释,可根据需要进行更改。 # -*- coding: utf-8 -*- import numpy as np ...
Python股票K线绘制方法,纯matplotlib绘制
主要介绍了Python使用matplotlib绘制Logistic曲线操作,结合实例形式详细分析了Python基于matplotlib库绘制Logistic曲线相关步骤与实现技巧,需要的朋友可以参考下
开发工具是jupyter notebook,利用matplotlib绘制发动机万有特性曲线。主要分为三部分绘制,绘制等燃油消耗曲线/等功率曲线/外特性曲线。压缩包中的图是根据实际采集到的发动机数据(出于保密,无法上传),采用多元...
——学习参考资料:仅用于个人学习使用! 本代码仅作学习交流,切勿用于商业用途,否则后果自负。若涉及侵权,请联系,会尽快处理! 未进行详尽测试,请自行调试!
小编之前也和大家分享过python使用matplotlib实现的折线图和制饼图效果,感兴趣的朋友们也可以点击查看,下面来看看python使用matplotlib绘制柱状图的方法吧,具体如下: 1. 基本的柱状图 import matplotlib.pyplot...
# Python使用matplotlib绘制动态图 制作动画 雨滴波纹动画 1. 使用 matplotlib 绘制50个随机位置、大小、透明度的圆形;...3. 通过matplotlib的FuncAnimation()合成动画并在界面中展示,也可保存为gif动图。
[Matplotlib科学绘图系列]使用Matplotlib绘制折线图 导入必要的库:我们需要导入matplotlib.pyplot库,以便进行数据可视化的工作。这个库非常常用,并且对于数据可视化有很强大的支持。我们将这个库简写为plt,以...
[Matplotlib科学绘图系列]使用Matplotlib绘制直方图 导入必要的库:我们需要导入matplotlib.pyplot库,以便进行数据可视化的工作。这个库非常常用,并且对于数据可视化有很强大的支持。我们将这个库简写为plt,以...
在 Python 中使用 Matplotlib 绘制甘特图(Gantt Chart)是一种可 视化项目进度和任务安排的方式。甘特图通常用于表示项目中任务的开始 和结束时间,以及任务之间的依赖关系。本文将介绍如何使用 Matplotlib 库在 ...
[Matplotlib科学绘图系列]使用Matplotlib绘制渐变散点图 导入必要的库:我们需要导入matplotlib.pyplot库,以便进行数据可视化的工作。这个库非常常用,并且对于数据可视化有很强大的支持。我们将这个库简写为plt,...
Matplotlib 2.x By Example 原版图书讲解使用python中的Matplotlib绘制各种图表,一书掌握图表绘制,绘制各种精美图表,进行数据分析。 出版日期: 2017年8月 主页:...
[Matplotlib科学绘图系列]使用Matplotlib绘制条形图 实现步骤 1、导入必要的库:我们需要导入matplotlib.pyplot库,以便进行数据可视化的工作。这个库非常常用,并且对于数据可视化有很强大的支持。我们将这个库简写...