matplotlib.pyplot.plot()参数使用详解


Posted in Python onJuly 28, 2020

在交互环境中查看帮助文档:

import matplotlib.pyplot as plt
help(plt.plot)

以下是对帮助文档重要部分的翻译:

plot函数的一般的调用形式:

#单条线:
plot([x], y, [fmt], data=None, **kwargs)
#多条线一起画
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)

可选参数[fmt] 是一个字符串来定义图的基本属性如:颜色(color),点型(marker),线型(linestyle),

具体形式  fmt = '[color][marker][line]'

fmt接收的是每个属性的单个字母缩写,例如:

plot(x, y, 'bo-') # 蓝色圆点实线

若属性用的是全名则不能用*fmt*参数来组合赋值,应该用关键字参数对单个属性赋值如:

plot(x,y2,color='green', marker='o', linestyle='dashed', linewidth=1, markersize=6)

plot(x,y3,color='#900302',marker='+',linestyle='-')

常见的颜色参数:**Colors**

也可以对关键字参数color赋十六进制的RGB字符串如 color='#900302'

============= ===============================
 character  color
 ============= ===============================
 ``'b'``   blue 蓝
 ``'g'``   green 绿
 ``'r'``   red 红
 ``'c'``   cyan 蓝绿
 ``'m'``   magenta 洋红
 ``'y'``   yellow 黄
 ``'k'``   black 黑
 ``'w'``   white 白
 ============= ===============================

点型参数**Markers**,如:marker='+' 这个只有简写,英文描述不被识别

============= ===============================
 character  description
 ============= ===============================
 ``'.'``   point marker
 ``','``   pixel marker
 ``'o'``   circle marker
 ``'v'``   triangle_down marker
 ``'^'``   triangle_up marker
 ``'<'``   triangle_left marker
 ``'>'``   triangle_right marker
 ``'1'``   tri_down marker
 ``'2'``   tri_up marker
 ``'3'``   tri_left marker
 ``'4'``   tri_right marker
 ``'s'``   square marker
 ``'p'``   pentagon marker
 ``'*'``   star marker
 ``'h'``   hexagon1 marker
 ``'H'``   hexagon2 marker
 ``'+'``   plus marker
 ``'x'``   x marker
 ``'D'``   diamond marker
 ``'d'``   thin_diamond marker
 ``'|'``   vline marker
 ``'_'``   hline marker
 ============= ===============================

线型参数**Line Styles**,linestyle='-'

============= ===============================
 character  description
 ============= ===============================
 ``'-'``   solid line style 实线
 ``'--'``   dashed line style 虚线
 ``'-.'``   dash-dot line style 点画线
 ``':'``   dotted line style 点线
 ============= ===============================

样例1

函数原型:matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)

>>> plot('xlabel', 'ylabel', data=obj)

解释:All indexable objects are supported. This could e.g. be a dict, a pandas.DataFame or a structured numpy array.

data 参数接受一个对象数据类型,所有可被索引的对象都支持,如 dict 等

import matplotlib.pyplot as plt 
import numpy as np
'''read file 
fin=open("para.txt")
a=[]
for i in fin:
 a.append(float(i.strip()))
a=np.array(a)
a=a.reshape(9,3)
'''
a=np.random.random((9,3))*2 #随机生成y
 
y1=a[0:,0]
y2=a[0:,1]
y3=a[0:,2]
 
x=np.arange(1,10)
 
ax = plt.subplot(111)
width=10
hight=3
ax.arrow(0,0,0,hight,width=0.01,head_width=0.1, head_length=0.3,length_includes_head=True,fc='k',ec='k')
ax.arrow(0,0,width,0,width=0.01,head_width=0.1, head_length=0.3,length_includes_head=True,fc='k',ec='k')
 
ax.axes.set_xlim(-0.5,width+0.2)
ax.axes.set_ylim(-0.5,hight+0.2)
 
plotdict = { 'dx': x, 'dy': y1 }
ax.plot('dx','dy','bD-',data=plotdict)
 
ax.plot(x,y2,'r^-')
ax.plot(x,y3,color='#900302',marker='*',linestyle='-')
 
plt.show()

matplotlib.pyplot.plot()参数使用详解

样例2,

import matplotlib.pyplot as plt 
import numpy as np 
 
x = np.arange(0, 2*np.pi, 0.02) 
y = np.sin(x) 
y1 = np.sin(2*x) 
y2 = np.sin(3*x) 
ym1 = np.ma.masked_where(y1 > 0.5, y1) 
ym2 = np.ma.masked_where(y2 < -0.5, y2) 
 
lines = plt.plot(x, y, x, ym1, x, ym2, 'o') 
#设置线的属性
plt.setp(lines[0], linewidth=1) 
plt.setp(lines[1], linewidth=2) 
plt.setp(lines[2], linestyle='-',marker='^',markersize=4) 
#线的标签
plt.legend(('No mask', 'Masked if > 0.5', 'Masked if < -0.5'), loc='upper right') 
plt.title('Masked line demo') 
plt.show()

matplotlib.pyplot.plot()参数使用详解

例3 :圆

import numpy as np
import matplotlib.pyplot as plt
 
theta = np.arange(0, 2*np.pi, 0.01)
xx = [1,2,3,10,15,8]
yy = [1,-1,0,0,7,0]
rr = [7,7,3,6,9,9]
 
fig = plt.figure()
axes = flg.add_subplot(111)
 
i = 0
while i < len(xx):
 x = xx[i] + rr[i] *np.cos(theta)
 x = xx[i] + rr[i] *np.cos(theta)
 axes.plot(x,y)
 axes.plot(xx[i], yy[i], color='#900302', marker='*')
  i = i+1
width = 20
hight = 20
axes.arrow(0,0,0,hight,width=0.01,head_width=0.1,head_length=0.3,fc='k',ec='k')
axes.arrow(0,0,width,0,width=0.01,head_width=0.1,head_length=0.3,fc='k',ec='k')
plt.show()

 到此这篇关于matplotlib.pyplot.plot()参数详解的文章就介绍到这了,更多相关matplotlib.pyplot.plot()内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python两个整数相除得到浮点数值的方法
Mar 18 Python
pymongo给mongodb创建索引的简单实现方法
May 06 Python
Python使用Phantomjs截屏网页的方法
May 17 Python
uwsgi+nginx部署Django项目操作示例
Dec 04 Python
关于Pycharm无法debug问题的总结
Jan 19 Python
Python学习笔记之读取文件、OS模块、异常处理、with as语法示例
Jun 04 Python
pyqt 实现在Widgets中显示图片和文字的方法
Jun 13 Python
Python Pandas 如何shuffle(打乱)数据
Jul 30 Python
使用 Python 清理收藏夹里已失效的网站
Dec 03 Python
Python 判断时间是否在时间区间内的实例
May 16 Python
Python2及Python3如何实现兼容切换
Sep 01 Python
python实现在列表中查找某个元素的下标示例
Nov 16 Python
matplotlib图例legend语法及设置的方法
Jul 28 #Python
Matplotlib中%matplotlib inline如何使用
Jul 28 #Python
Python基于xlrd模块处理合并单元格
Jul 28 #Python
Python 在函数上添加包装器
Jul 28 #Python
Python matplotlib图例放在外侧保存时显示不完整问题解决
Jul 28 #Python
Python 如何反方向迭代一个序列
Jul 28 #Python
Python Matplotlib简易教程(小白教程)
Jul 28 #Python
You might like
PHP开发文件系统实例讲解
2006/10/09 PHP
PHP程序员编程注意事项
2008/04/10 PHP
PHP 基于Yii框架中使用smarty模板的方法详解
2013/06/13 PHP
php curl选项列表(超详细)
2013/07/01 PHP
PHP变量的定义、可变变量、变量引用、销毁方法
2013/12/20 PHP
php继承中方法重载(覆盖)的应用场合
2015/02/09 PHP
php连接oracle数据库的核心步骤
2016/05/26 PHP
js限制文本框只能输入数字(正则表达式)
2012/07/15 Javascript
javascript文件中引用依赖的js文件的方法
2014/03/17 Javascript
上传图片js判断图片尺寸和格式兼容IE
2014/09/01 Javascript
在JavaScript中处理时间之getHours()方法的使用
2015/06/10 Javascript
PHP+jQuery+Ajax+Mysql如何实现发表心情功能
2015/08/06 Javascript
jquery原理以及学习技巧介绍
2015/11/11 Javascript
详解javascript中原始数据类型Null和Undefined
2015/12/17 Javascript
Bootstrap每天必学之级联下拉菜单
2016/03/27 Javascript
深入浅析JavaScript中with语句的理解
2016/05/12 Javascript
js关于getImageData跨域问题的解决方法
2016/10/14 Javascript
js学习心得_一个简单的动画库封装tween.js
2017/07/14 Javascript
Vue项目页面跳转时浏览器窗口上方显示进度条功能
2020/03/26 Javascript
javascript设计模式 ? 访问者模式原理与用法实例分析
2020/04/26 Javascript
JavaScript使用prototype属性实现继承操作示例
2020/05/22 Javascript
解决antd 表单设置默认值initialValue后验证失效的问题
2020/11/02 Javascript
[54:45]2018DOTA2亚洲邀请赛 4.1 小组赛 A组 Optic vs OG
2018/04/02 DOTA
美国家喻户晓的保健品品牌:Vitamin World(维他命世界)
2016/08/19 全球购物
Sneaker Studio匈牙利:购买运动鞋
2018/03/26 全球购物
android面试问题与答案
2016/12/27 面试题
硕士研究生自我鉴定
2013/11/08 职场文书
《我的伯父鲁迅先生》教学反思
2014/02/12 职场文书
公司委托书范本
2014/04/04 职场文书
项目合作协议书
2014/04/16 职场文书
业务员岗位职责
2015/02/03 职场文书
事业单位个人总结
2015/02/12 职场文书
幼儿园园长工作总结2015
2015/05/25 职场文书
地震捐款简报
2015/07/21 职场文书
深入理解MySQL中MVCC与BufferPool缓存机制
2022/05/25 MySQL
Go本地测试解耦任务拆解及沟通详解Go本地测试的思路沟通的重要性总结
2022/06/21 Golang