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实现简单socket程序在两台电脑之间传输消息的方法
Mar 13 Python
Python实现随机生成有效手机号码及身份证功能示例
Jun 05 Python
利用Python批量压缩png方法实例(支持过滤个别文件与文件夹)
Jul 30 Python
解决python nohup linux 后台运行输出的问题
May 11 Python
深入解析python中的实例方法、类方法和静态方法
Mar 11 Python
python 批量添加的button 使用同一点击事件的方法
Jul 17 Python
Python Web框架之Django框架cookie和session用法分析
Aug 16 Python
Django学习之文件上传与下载
Oct 06 Python
wxPython实现画图板
Aug 27 Python
PyQt5 closeEvent关闭事件退出提示框原理解析
Jan 08 Python
python matplotlib画盒图、子图解决坐标轴标签重叠的问题
Jan 19 Python
Python decimal模块使用方法详解
Jun 08 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登陆后跳转到登陆前页面实现思路及代码
2014/01/17 PHP
php强制运行广告的方法
2014/12/01 PHP
PHP实现自动识别原编码并对字符串进行编码转换的方法
2016/07/13 PHP
php中输出json对象的值(实现方法)
2018/03/07 PHP
fromCharCode和charCodeAt 方法
2006/12/27 Javascript
根据一段代码浅谈Javascript闭包
2010/12/14 Javascript
JS打开新窗口的2种方式
2013/04/18 Javascript
jquery日历控件实现方法分享
2014/03/07 Javascript
jQuery层动画定位滑动效果的方法
2015/04/30 Javascript
jQuery里filter()函数与find()函数用法分析
2015/06/24 Javascript
jQuery动态星级评分效果实现方法
2015/08/06 Javascript
javascript实现显示和隐藏div方法汇总
2015/08/14 Javascript
jQuery实现查找最近父节点的方法
2016/06/23 Javascript
使用Ajax与服务器(JSON)通信实例
2016/11/04 Javascript
js实现文字列表无缝滚动效果
2017/06/23 Javascript
webpack vue项目开发环境局域网访问方法
2018/03/20 Javascript
详解Element 指令clickoutside源码分析
2019/02/15 Javascript
vue图片上传本地预览组件使用详解
2019/02/20 Javascript
JavaScript函数式编程(Functional Programming)纯函数用法分析
2019/05/22 Javascript
vue分页插件的使用方法
2019/12/25 Javascript
解决Vue中的生命周期beforeDestory不触发的问题
2020/07/21 Javascript
[03:08]Ti4观战指南上
2014/07/07 DOTA
用tensorflow构建线性回归模型的示例代码
2018/03/05 Python
Python实现获取nginx服务器ip及流量统计信息功能示例
2018/05/18 Python
python 3.6.2 安装配置方法图文教程
2018/09/18 Python
Django实现简单网页弹出警告代码
2019/11/15 Python
浅谈selenium如何应对网页内容需要鼠标滚动加载的问题
2020/03/14 Python
如何使用Python自动生成报表并以邮件发送
2020/10/15 Python
请解释virtual关键字的含义
2015/06/17 面试题
护士思想汇报
2014/01/12 职场文书
学生会主席竞聘书
2014/03/31 职场文书
文明城市创建标语
2014/06/16 职场文书
2015欢度元旦标语口号
2014/12/09 职场文书
离职感谢信怎么写
2015/01/22 职场文书
黄山导游词
2015/01/31 职场文书
2016年大学生就业指导课心得体会
2015/10/09 职场文书