首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

matplotlib.pyplot.plot()参数详解

  • 23-09-25 06:21
  • 3162
  • 14008
blog.csdn.net

https://matplotlib.org/api/pyplot_summary.html

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

  1. import matplotlib.pyplot as plt
  2. help(plt.plot)

以下是对帮助文档重要部分的翻译:

plot函数的一般的调用形式:

  1. #单条线:
  2. plot([x], y, [fmt], data=None, **kwargs)
  3. #多条线一起画
  4. 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'

  1. ============= ===============================
  2. character color
  3. ============= ===============================
  4. ``'b'`` blue 蓝
  5. ``'g'`` green 绿
  6. ``'r'`` red 红
  7. ``'c'`` cyan 蓝绿
  8. ``'m'`` magenta 洋红
  9. ``'y'`` yellow 黄
  10. ``'k'`` black 黑
  11. ``'w'`` white 白
  12. ============= ===============================
 点型参数**Markers**,如:marker='+' 这个只有简写,英文描述不被识别
  1. ============= ===============================
  2. character description
  3. ============= ===============================
  4. ``'.'`` point marker
  5. ``','`` pixel marker
  6. ``'o'`` circle marker
  7. ``'v'`` triangle_down marker
  8. ``'^'`` triangle_up marker
  9. ``'<'`` triangle_left marker
  10. ``'>'`` triangle_right marker
  11. ``'1'`` tri_down marker
  12. ``'2'`` tri_up marker
  13. ``'3'`` tri_left marker
  14. ``'4'`` tri_right marker
  15. ``'s'`` square marker
  16. ``'p'`` pentagon marker
  17. ``'*'`` star marker
  18. ``'h'`` hexagon1 marker
  19. ``'H'`` hexagon2 marker
  20. ``'+'`` plus marker
  21. ``'x'`` x marker
  22. ``'D'`` diamond marker
  23. ``'d'`` thin_diamond marker
  24. ``'|'`` vline marker
  25. ``'_'`` hline marker
  26. ============= ===============================

线型参数**Line Styles**,linestyle='-'

  1. ============= ===============================
  2. character description
  3. ============= ===============================
  4. ``'-'`` solid line style 实线
  5. ``'--'`` dashed line style 虚线
  6. ``'-.'`` dash-dot line style 点画线
  7. ``':'`` dotted line style 点线
  8. ============= ===============================

样例1

  1. 函数原型:matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)
  2. >>> plot('xlabel', 'ylabel', data=obj)
  3. 解释:All indexable objects are supported. This could e.g. be a dict, a pandas.DataFame or a structured numpy array.
  4. data 参数接受一个对象数据类型,所有可被索引的对象都支持,如 dict 等
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. '''read file
  4. fin=open("para.txt")
  5. a=[]
  6. for i in fin:
  7. a.append(float(i.strip()))
  8. a=np.array(a)
  9. a=a.reshape(9,3)
  10. '''
  11. a=np.random.random((9,3))*2 #随机生成y
  12. y1=a[0:,0]
  13. y2=a[0:,1]
  14. y3=a[0:,2]
  15. x=np.arange(1,10)
  16. ax = plt.subplot(111)
  17. width=10
  18. hight=3
  19. 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')
  20. 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')
  21. ax.axes.set_xlim(-0.5,width+0.2)
  22. ax.axes.set_ylim(-0.5,hight+0.2)
  23. plotdict = { 'dx': x, 'dy': y1 }
  24. ax.plot('dx','dy','bD-',data=plotdict)
  25. ax.plot(x,y2,'r^-')
  26. ax.plot(x,y3,color='#900302',marker='*',linestyle='-')
  27. plt.show()

样例2,

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. x = np.arange(0, 2*np.pi, 0.02)
  4. y = np.sin(x)
  5. y1 = np.sin(2*x)
  6. y2 = np.sin(3*x)
  7. ym1 = np.ma.masked_where(y1 > 0.5, y1)
  8. ym2 = np.ma.masked_where(y2 < -0.5, y2)
  9. lines = plt.plot(x, y, x, ym1, x, ym2, 'o')
  10. #设置线的属性
  11. plt.setp(lines[0], linewidth=1)
  12. plt.setp(lines[1], linewidth=2)
  13. plt.setp(lines[2], linestyle='-',marker='^',markersize=4)
  14. #线的标签
  15. plt.legend(('No mask', 'Masked if > 0.5', 'Masked if < -0.5'), loc='upper right')
  16. plt.title('Masked line demo')
  17. plt.show()

例3 :圆

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. theta = np.arange(0, 2*np.pi, 0.01)
  4. xx = [1,2,3,10,15,8]
  5. yy = [1,-1,0,0,7,0]
  6. rr = [7,7,3,6,9,9]
  7. fig = plt.figure()
  8. axes = flg.add_subplot(111)
  9. i = 0
  10. while i < len(xx):
  11. x = xx[i] + rr[i] *np.cos(theta)
  12. x = xx[i] + rr[i] *np.cos(theta)
  13. axes.plot(x,y)
  14. axes.plot(xx[i], yy[i], color='#900302', marker='*')
  15. i = i+1
  16. width = 20
  17. hight = 20
  18. axes.arrow(0,0,0,hight,width=0.01,head_width=0.1,head_length=0.3,fc='k',ec='k')
  19. axes.arrow(0,0,width,0,width=0.01,head_width=0.1,head_length=0.3,fc='k',ec='k')
  20. plt.show()

 

 

文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树绘图库MatplotlibMatplotlib快速入门336432 人正在系统学习中
注:本文转载自blog.csdn.net的ims-的文章"https://blog.csdn.net/sinat_36219858/article/details/79800460"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2491) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top