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 相关文章推荐
解决windows下Sublime Text 2 运行 PyQt 不显示的方法分享
Jun 18 Python
python判断字符串是否是json格式方法分享
Nov 07 Python
PyQt5每天必学之带有标签的复选框
Apr 19 Python
解决pip install xxx报错SyntaxError: invalid syntax的问题
Nov 30 Python
Python基本数据结构与用法详解【列表、元组、集合、字典】
Mar 23 Python
python实现得到当前登录用户信息的方法
Jun 21 Python
Python一键安装全部依赖包的方法
Aug 12 Python
python 使用pdfminer3k 读取PDF文档的例子
Aug 27 Python
感知器基础原理及python实现过程详解
Sep 30 Python
Python Django中间件使用原理及流程分析
Jun 13 Python
pymysql模块使用简介与示例
Nov 17 Python
python 利用 PIL 将数组值转成图片的实现
Apr 12 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中类的继承和用法实例分析
2016/05/24 PHP
js点击更换背景颜色或图片的实例代码
2013/06/25 Javascript
JS复制内容到剪切板的实例代码(兼容IE与火狐)
2013/11/19 Javascript
nodejs下打包模块archiver详解
2014/12/03 NodeJs
JavaScript中实现单体模式分享
2015/01/29 Javascript
jQuery模拟新浪微博首页滚动效果的方法
2015/03/11 Javascript
Javascript调用函数方法的几种方式介绍
2015/03/20 Javascript
jquery中$each()方法的使用指南
2015/04/30 Javascript
微信小程序加载更多 点击查看更多
2016/11/29 Javascript
微信小程序 欢迎界面开发的实例详解
2016/11/30 Javascript
用纯Node.JS弹出Windows系统消息提示框实例(MessageBox)
2017/05/17 Javascript
Angularjs的启动过程分析
2017/07/18 Javascript
react native仿微信PopupWindow效果的实例代码
2017/08/07 Javascript
ajax+node+request爬取网络图片的实例(宅男福利)
2017/08/28 Javascript
Angular js 实现添加用户、修改密码、敏感字、下拉菜单的综合操作方法
2017/10/24 Javascript
Vue中如何实现proxy代理
2018/04/20 Javascript
python覆盖写入,追加写入的实例
2019/06/26 Python
python 求某条线上特定x值或y值的点坐标方法
2019/07/09 Python
使用Python和OpenCV检测图像中的物体并将物体裁剪下来
2019/10/30 Python
查看jupyter notebook每个单元格运行时间实例
2020/04/22 Python
丝芙兰巴西官方商城:SEPHORA巴西
2016/10/31 全球购物
英国家用电器折扣网站:Electrical Discount UK
2018/09/17 全球购物
将"引用"作为函数返回值类型的格式、好处和需要遵守的规则
2016/02/09 面试题
什么叫应用程序域?什么是受管制的代码?什么是强类型系统?什么是装箱和拆箱?
2016/08/13 面试题
逻辑链路控制协议
2016/10/01 面试题
XMLHttpRequest对象在IE和Firefox中创建方式有没有不同
2016/03/23 面试题
社区巾帼文明岗事迹材料
2014/06/03 职场文书
大学运动会加油稿200字(5篇)
2014/09/27 职场文书
个人委托函范文
2015/01/29 职场文书
董事长岗位职责
2015/02/13 职场文书
2015最新民情日记范文
2015/06/26 职场文书
关于环保的宣传稿
2015/07/23 职场文书
python常见的占位符总结及用法
2021/07/02 Python
使用goaccess分析nginx日志的详细方法
2021/07/09 Servers
Win11 引入 Windows 365 云操作系统,适应疫情期间混合办公模式:启动时直接登录、模
2022/04/06 数码科技
MongoDB数据库之添删改查
2022/04/26 MongoDB