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 相关文章推荐
python3编码问题汇总
Sep 06 Python
Python基于回溯法子集树模板解决m着色问题示例
Sep 07 Python
浅谈Python中range和xrange的区别
Dec 20 Python
python+django+sql学生信息管理后台开发
Jan 11 Python
基于Django与ajax之间的json传输方法
May 29 Python
详解python3 + Scrapy爬虫学习之创建项目
Apr 12 Python
django项目中使用手机号登录的实例代码
Aug 15 Python
Python传递参数的多种方式(小结)
Sep 18 Python
pytorch 中pad函数toch.nn.functional.pad()的用法
Jan 08 Python
python实现双人五子棋(终端版)
Dec 30 Python
Python爬虫scrapy框架Cookie池(微博Cookie池)的使用
Jan 13 Python
Python django中如何使用restful框架
Jun 23 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
YB217、YB235、YB400浅听
2021/03/02 无线电
php下HTTP Response中的Chunked编码实现方法
2008/11/19 PHP
php 代码优化之经典示例
2011/03/24 PHP
PHP实例分享判断客户端是否使用代理服务器及其匿名级别
2014/06/04 PHP
PHP循环遍历数组的3种方法list()、each()和while总结
2014/11/19 PHP
javascript定时保存表单数据的代码
2011/03/17 Javascript
Js 冒泡事件阻止实现代码
2013/01/27 Javascript
兼容FF和IE的动态table示例自写
2013/10/21 Javascript
让浏览器崩溃的12行JS代码(DoS攻击分析及防御)
2016/10/10 Javascript
Bootstrap选项卡动态切换效果
2016/11/28 Javascript
vue实现简单实时汇率计算功能
2017/01/15 Javascript
对vux点击事件的优化详解
2018/08/28 Javascript
在layer弹层layer.prompt中,修改placeholder的实现方法
2019/09/27 Javascript
[45:14]Optic vs VP 2018国际邀请赛淘汰赛BO3 第二场 8.24
2018/08/25 DOTA
用Python进行基础的函数式编程的教程
2015/03/31 Python
Python 含参构造函数实例详解
2017/05/25 Python
Python写捕鱼达人的游戏实现
2020/03/31 Python
python网络编程:socketserver的基本使用方法实例分析
2020/04/09 Python
OpenCV4.1.0+VS2017环境配置的方法步骤
2020/07/09 Python
python map比for循环快在哪
2020/09/21 Python
利用CSS3实现开门效果实例源码
2016/08/22 HTML / CSS
有关HTML5页面在iPhoneX适配问题
2017/11/13 HTML / CSS
Weekendesk意大利:探索多种引人入胜的周末主题
2016/10/14 全球购物
Monnier Frères美国官网:法国知名奢侈品网站
2016/11/22 全球购物
吉尔德利巧克力公司:Ghirardelli Chocolate Company
2019/03/27 全球购物
甜点店创业计划书
2014/01/27 职场文书
毕业生就业意向书
2014/04/01 职场文书
党的群众路线教育实践活动查摆剖析材料
2014/10/10 职场文书
关于运动会的广播稿50字
2014/10/17 职场文书
见习报告格式要求
2014/11/04 职场文书
电影雨中的树观后感
2015/06/15 职场文书
少先队入队仪式主持词
2015/07/04 职场文书
高中16字霸气押韵班级口号集锦!
2019/06/27 职场文书
Nginx优化服务之网页压缩的实现方法
2021/03/31 Servers
Python制作动态字符画的源码
2021/08/04 Python
Java8中Stream的一些神操作
2021/11/02 Java/Android