pyplot

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. 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. ax.plot(x,y1, 'bD-',data=[ 'lwww'])
  24. ax.plot(x,y2, 'r^-')
  25. ax.plot(x,y3,color= '#900302',marker= '*',linestyle= '-')
  26. 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()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值