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

 更新时间:2020年07月28日 11:14:25   作者:ims-  
这篇文章主要介绍了matplotlib.pyplot.plot()参数详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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

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()

样例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()

例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实现逐个读取txt字符并修改

    python实现逐个读取txt字符并修改

    今天小编就为大家分享一篇python实现逐个读取txt字符并修改,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • Linux下python与C++使用dlib实现人脸检测

    Linux下python与C++使用dlib实现人脸检测

    这篇文章主要为大家详细介绍了Linux下python与C++使用dlib实现人脸检测,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • 简单介绍Python中的struct模块

    简单介绍Python中的struct模块

    这篇文章主要介绍了Python中的struct模块,代码基于Python2.x版本,需要的朋友可以参考下
    2015-04-04
  • Python中使用Selenium环境安装的方法步骤

    Python中使用Selenium环境安装的方法步骤

    这篇文章主要介绍了Python中使用Selenium环境安装的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • python实现自幂数的示例代码

    python实现自幂数的示例代码

    这篇文章主要介绍了python实现自幂数的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • Python实现解析路径字符串并获取每个文件夹名称

    Python实现解析路径字符串并获取每个文件夹名称

    在 Python 中,解析路径字符串并获取每个文件夹的名称是一项常见的任务,这篇文章主要为大家详细介绍了Python解析路径字符串的具体方法,希望对大家有所帮助
    2024-04-04
  • Python机器学习之决策树和随机森林

    Python机器学习之决策树和随机森林

    本文主要介绍了机器学习之决策树和随机森林,详细的介绍了实现 原理机器实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • pycharm中加了断点却无法调试,直接执行到程序结束如何解决

    pycharm中加了断点却无法调试,直接执行到程序结束如何解决

    这篇文章主要介绍了pycharm中加了断点却无法调试,直接执行到程序结束如何解决问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Python 如何引用不确定的函数

    Python 如何引用不确定的函数

    在Python中,引用不确定的函数通常意味着我们可能在运行时才知道要调用哪个函数,或者我们可能想根据某些条件动态地选择不同的函数来执行,下面给大家分享Python 如何引用不确定的函数,感兴趣的朋友跟随小编一起看看吧
    2024-07-07
  • Python基础知识快速上手入门学习

    Python基础知识快速上手入门学习

    本篇文章使用代码示例,一看就会,从基础语法、变量类型、运算符和条件语句多个方面详细阐述了Python基础知识快速上手入门学习的内容,希望本文能对Python初学者有所帮助
    2023-08-08

最新评论