python – 更改matplotlib线样式中图

前端之家收集整理的这篇文章主要介绍了python – 更改matplotlib线样式中图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在绘制一些数据(两行),我想更改线条部分的线条样式,它们之间的差异具有统计意义.所以,在下面的图像(现在一个链接b / c反垃圾邮件政策不允许我发布一个图像)我希望线条看起来不同(也许是虚线)直到他们开始收敛在大约35 x轴.

line plot

有没有办法轻松做到这一点?我有x轴的值有差异是显着的,我不清楚如何在某些x轴位置更改线样式.

解决方法

编辑:我已经这样开放了,所以我没有注意到里卡多的回答.因为matplotlib会将事物转换为numpy数组,无论如何,都有更有效的方法来实现.

举个例子:

只是绘制两条不同的线条,一条是一条虚线,另一条是一条坚固的线条.

例如.

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. x = np.linspace(0,10,100)
  5. y1 = 2 * x
  6. y2 = 3 * x
  7.  
  8. xthresh = 4.5
  9. diff = np.abs(y1 - y2)
  10. below = diff < xthresh
  11. above = diff >= xthresh
  12.  
  13. # Plot lines below threshold as dotted...
  14. plt.plot(x[below],y1[below],'b--')
  15. plt.plot(x[below],y2[below],'g--')
  16.  
  17. # Plot lines above threshold as solid...
  18. plt.plot(x[above],y1[above],'b-')
  19. plt.plot(x[above],y2[above],'g-')
  20.  
  21. plt.show()

对于循环的情况,使用掩码数组:

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. x = np.linspace(0,100)
  5. y1 = 2 * np.cos(x)
  6. y2 = 3 * np.sin(x)
  7.  
  8. xthresh = 2.0
  9. diff = np.abs(y1 - y2)
  10. below = diff < xthresh
  11. above = diff >= xthresh
  12.  
  13. # Plot lines below threshold as dotted...
  14. plt.plot(np.ma.masked_where(below,x),np.ma.masked_where(below,y1),'b--')
  15. plt.plot(np.ma.masked_where(below,y2),'g--')
  16.  
  17. # Plot lines above threshold as solid...
  18. plt.plot(np.ma.masked_where(above,np.ma.masked_where(above,'b-')
  19. plt.plot(np.ma.masked_where(above,'g-')
  20.  
  21. plt.show()

猜你在找的Python相关文章