pyplot 作图总结
折线图
下面是绘制折线图,设置图片的横轴纵轴标签,图片标题的API的用法。
import matplotlib.pyplot as pyplot
# init
pyplot.figure()
# arguments
x_label = 'X-label'
y_label = 'Y-label'
title = 'Demo-title'
# points data
x = [1, 2, 3, 4]
y = [45, 32, 46, 89]
# set arguments
pyplot.xlabel(x_label)
pyplot.ylabel(y_label)
pyplot.title(title)
# set data
pyplot.plot(x, y)
pyplot.show()
多个函数图像
import matplotlib.pyplot as pyplot
import numpy
# init
pyplot.figure()
# arguments
x_label = 'x'
y_label = 'sin(x)'
title = 'Figure-sin(x)'
# points data
# [0, 10] 区间内的 1000 个均匀分布的 x
x = numpy.linspace(0, 10, 1000)
sin_y = numpy.sin(x)
cos_y = numpy.cos(x)
# set arguments
pyplot.xlabel(x_label)
pyplot.ylabel(y_label)
pyplot.title(title)
# 设置 y 轴范围
pyplot.ylim(-1.5, 1.5)
# set data
# label, color, linewidth 是 图示 参数,用于区分多个曲线的情况
pyplot.plot(x, sin_y, label='$ sin(x) $', color='red', linewidth=1)
pyplot.plot(x, cos_y, label='$ cos(x) $', color='blue', linewidth=1)
pyplot.legend()
pyplot.show()
多个函数图像 2.0
在上述基础上进一步封装, 对 draw_arguments
进行实例化, 然后调用 draw_figure
即可.
import matplotlib.pyplot as pyplot
import numpy
import math
class draw_arguments:
def __init__(self, func, func_name, x_domain: tuple, points_num=1000):
super().__init__()
self.draw_func = func
self.func_name = func_name
self.x_data = numpy.linspace(x_domain[0], x_domain[1], points_num)
self.y_data = [func(x) for x in self.x_data]
def draw_figure(dargs, title='Figure', x_label='x', y_label='y'):
# init
pyplot.figure()
# set arguments
pyplot.xlabel(x_label)
pyplot.ylabel(y_label)
pyplot.title(title)
# set data
# label, color, linewidth 是 图示 参数,用于区分多个曲线的情况
for draw in dargs:
pyplot.plot(draw.x_data, draw.y_data, label='$' +
draw.func_name + '$', linewidth=1)
pyplot.legend()
pyplot.show()
d1 = draw_arguments(func=lambda x: 2**x,
func_name='2^x',
x_domain=(0, 5))
d2 = draw_arguments(func=lambda x: x*x,
func_name='x^2',
x_domain=(0, 5))
draw_figure([d1, d2])
绘制动画
心形曲线
提到心形曲线, 最著名的莫过于笛卡尔心形曲线, 其方程为(极坐标的形式):
\]
但我不是很想用这个.
还有一个较为著名的方程形式的心形曲线:
\]
也不是很想用, 因为这都不是函数形式.
偶然发现了一个心形曲线为:
\]
其中, \(k \ge 10\) 时, 随着 \(k\) 的增大, 函数图像会越来趋近于一个心形.
当 \(k=10\) 时:
闲着没事, 用 python
做了一段动画:
import matplotlib.pyplot as pyplot
import numpy
import math
pyplot.rcParams['figure.figsize'] = (3, 3) # 图像显示大小
pyplot.rcParams['lines.linewidth'] = 1.5 # 设置曲线线条宽度
pyplot.ion()
data = numpy.linspace(-math.sqrt(math.pi), math.sqrt(math.pi), 500)
x, y = [], []
def heart(x):
return math.pow(x * x, 1 / 3) + math.sqrt(math.pi - x * x) * math.sin(10 * x * math.pi)
for k in data:
x.append(k)
y.append(heart(k))
pyplot.clf()
subplot = pyplot.subplot()
pyplot.plot(x, y)
pyplot.pause(0.0000001)
pyplot.ioff()
pyplot.show()
pyplot 作图总结的更多相关文章
- Python#常用的模块和简单用法
目录 random 随机模块 os 文件夹模块: time 时间模块: matplotlab.pyplot 作图模块 mpl_toolkits.mplot3d 绘制3D图模块 Pygame Reque ...
- Python与R的区别和联系
转自:http://bbs.pinggu.org/thread-3078817-1-1.html 有人说Python和R的区别是显而易见的,因为R是针对统计的,python是给程序员设计的,其实这话对 ...
- 6 python高级数据处理和可视化
6.2. pyplot作图 1.折线图和散点图 t = np.arange(0,4,0.1) plt.plot(t,t,'o',t,t+2,t,t**2,'o') plt.show() 2.柱线图 p ...
- Matplotlib数据可视化(1):入门介绍
1 matplot入门指南¶ matplotlib是Python科学计算中使用最多的一个可视化库,功能丰富,提供了非常多的可视化方案,基本能够满足各种场景下的数据可视化需求.但功能丰富从另一方面来 ...
- matplotlib画图实例:pyplot、pylab模块及作图參数
http://blog.csdn.net/pipisorry/article/details/40005163 Matplotlib.pyplot画图实例 {使用pyplot模块} matplotli ...
- Python: 作图
在python中实现数据的可视化,也即作图,一般是依赖matplotlib宏包实现的.但常见的代码中都是加载pylab,是不是这里写错了呀?其实pylib只是matplotlib的一个模块,只是被做成 ...
- matplotlib 入门之Pyplot tutorial
文章目录 pyplot 介绍 修饰你的图案 格式字符串 [color][marker][line] Colors Markers Line Styles 利用关键字作图(大概是数据映射到属性吧) 传入 ...
- 用matplotlib获取雅虎股票数据并作图
matplotlib有一个finance子模块提供了一个获取雅虎股票数据的api接口:quotes_historical_yahoo_ochl 感觉非常好用! 示例一 获取数据并作折线图 import ...
- 一个简单的使用matplotlib作图的例子
#使用matplotlib作图 import numpy as np import matplotlib.pyplot as plt #x = np.linspace(start, stop, num ...
随机推荐
- 带你学习ES5中新增的方法
1. ES5中新增了一些方法,可以很方便的操作数组或者字符串,这些方法主要包括以下几个方面 数组方法 字符串方法 对象方法 2. 数组方法 迭代遍历方法:forEach().map().filter( ...
- 002.使用kubeadm安装kubernetes 1.17.0
一 环境准备 1.1 环境说明 master 192.168.132.131 docker-server1 node1 192.168.132.132 doc ...
- 达拉草201771010105《面向对象程序设计(java)》第六周学习总结
达拉草201771010105<面向对象程序设计(java)>第六周学习总结 第一部分:理论知识 1.类.超类和子类 类继承的格式: class 新类名extends已有类名一般来说,子类 ...
- 连接器巨头Molex莫仕:替代品厂PK原厂
序言:在中国电子产业,原厂PK替代品厂一直是一个极具话题性.美国在贸易战背景下,挤压中国的发展空间,迫使这一类企业要觉醒.当然受影响的不止中国电子企业,美国电子企业也一样. 在连接器这一领域,Mole ...
- JAVA GC算法详解
生存还是死亡 对象是否需要被垃圾收集器回收主要有两种方式:引用计数法和可达性分析算法 引用计数法 给对象添加一个引用计数器,每当有一个地方引用他的时候,计数器的数值就+1,当引用失效时,计数器就-1: ...
- JS基础入门篇(十八)—日期对象
1.日期对象 日期对象: 通过new Date()就能创建一个日期对象,这个对象中有当前系统时间的所有详细信息. 以下代码可以获取当前时间: <script> var t = new Da ...
- Xcode辅助工具之热重载插件利器
该博客首发于github.io 2018-06-13 13:43:44 文章最新修改于: 2019-03-31 13:47:20 昨天刚刚看完iOSTips微信公众号推送的文章, Injection: ...
- Java Opencv 实现 中值滤波器
原理 Note 以下原理来源于Richard Szeliski 的著作 Computer Vision: Algorithms and Applications 以及 Learning OpenCV ...
- vs远程调试iis
1.在开发电脑上 找到 D:\Software\VS2010\Common7\IDE\Remote Debugger 下面msvsmon.exe所在的两个文件夹x86和x64,使用x86或者x64是根 ...
- 2019-分享数百个 HT 工业互联网 2D 3D 可视化应用案例分享
继<分享数百个 HT 工业互联网 2D 3D 可视化应用案例>2018 篇,图扑软件定义 2018 为国内工业互联网可视化的元年后,2019 年里我们与各行业客户进行了更深度合作,拓展了H ...