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 字典(dict)遍历的四种方法性能测试报告
Jun 25 Python
Python Sleep休眠函数使用简单实例
Feb 02 Python
Python解析树及树的遍历
Feb 03 Python
selenium+python实现1688网站验证码图片的截取功能
Aug 14 Python
Python的条件表达式和lambda表达式实例
Jan 31 Python
Django重置migrations文件的方法步骤
May 01 Python
Python中zip()函数的简单用法举例
Sep 02 Python
python2和python3应该学哪个(python3.6与python3.7的选择)
Oct 01 Python
如何基于Python实现数字类型转换
Feb 07 Python
python爬虫scrapy框架的梨视频案例解析
Feb 20 Python
Django如何与Ajax交互
Apr 29 Python
告别网页搜索!教你用python实现一款属于自己的翻译词典软件
Jun 03 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/11/27 PHP
PHP中函数rand和mt_rand的区别比较
2012/12/26 PHP
推荐一款MAC OS X 下php集成开发环境mamp
2014/11/08 PHP
使用Zookeeper分布式部署PHP应用程序
2019/03/15 PHP
JavaScript中判断函数是new还是()调用的区别说明
2011/04/07 Javascript
javascript读写XML实现广告轮换(兼容IE、FF)
2013/08/09 Javascript
JavaScript SetInterval与setTimeout使用方法详解
2013/11/15 Javascript
angularjs学习笔记之完整的项目结构
2015/09/26 Javascript
JavaScript+html5 canvas实现图片破碎重组动画特效
2016/02/22 Javascript
js控制一个按钮是否可点击(可使用)disabled的实例
2017/02/14 Javascript
用js屏蔽被http劫持的浮动广告实现方法
2017/08/10 Javascript
jquery如何实现点击空白处隐藏元素
2017/12/05 jQuery
javaScript手机号码校验工具类PhoneUtils详解
2017/12/08 Javascript
Vuex实现计数器以及列表展示效果
2018/03/10 Javascript
React Router V4使用指南(精讲)
2018/09/17 Javascript
bootstrap-table实现表头固定以及列固定的方法示例
2019/03/07 Javascript
JS实现打字游戏
2019/12/17 Javascript
[02:01]大师之路——DOTA2完美大师赛11月论剑上海
2017/11/06 DOTA
Python实现的读取/更改/写入xml文件操作示例
2018/08/30 Python
python使用for循环计算0-100的整数的和方法
2019/02/01 Python
python集合常见运算案例解析
2019/10/17 Python
python使用paramiko实现ssh的功能详解
2020/03/06 Python
python GUI库图形界面开发之PyQt5动态(可拖动控件大小)布局控件QSplitter详细使用方法与实例
2020/03/06 Python
python中if及if-else如何使用
2020/06/02 Python
html5 乒乓球(碰撞检测)实例二
2013/07/25 HTML / CSS
求职毕业生自荐书
2014/02/08 职场文书
公司晚会主持词
2014/03/22 职场文书
水电站项目建议书
2014/05/12 职场文书
银行纠风工作实施方案
2014/06/08 职场文书
大学学雷锋活动总结
2014/06/26 职场文书
中药学专业毕业生推荐信
2014/07/10 职场文书
生活部的活动方案
2014/08/19 职场文书
2014年实习期工作总结
2014/11/27 职场文书
硕士学位申请报告
2015/05/15 职场文书
教师学期述职自我鉴定
2019/08/16 职场文书
基于Redis的List实现特价商品列表功能
2021/08/30 Redis