我正在绘制一些数据(两行),我想更改线条部分的线条样式,它们之间的差异具有统计意义.所以,在下面的图像(现在一个链接b / c反垃圾邮件政策不允许我发布一个图像)我希望线条看起来不同(也许是虚线)直到他们开始收敛在大约35 x轴.
有没有办法轻松做到这一点?我有x轴的值有差异是显着的,我不清楚如何在某些x轴位置更改线样式.
解决方法
编辑:我已经这样开放了,所以我没有注意到里卡多的回答.因为matplotlib会将事物转换为numpy数组,无论如何,都有更有效的方法来实现.
举个例子:
只是绘制两条不同的线条,一条是一条虚线,另一条是一条坚固的线条.
例如.
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0,10,100) y1 = 2 * x y2 = 3 * x xthresh = 4.5 diff = np.abs(y1 - y2) below = diff < xthresh above = diff >= xthresh # Plot lines below threshold as dotted... plt.plot(x[below],y1[below],'b--') plt.plot(x[below],y2[below],'g--') # Plot lines above threshold as solid... plt.plot(x[above],y1[above],'b-') plt.plot(x[above],y2[above],'g-') plt.show()
对于循环的情况,使用掩码数组:
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0,100) y1 = 2 * np.cos(x) y2 = 3 * np.sin(x) xthresh = 2.0 diff = np.abs(y1 - y2) below = diff < xthresh above = diff >= xthresh # Plot lines below threshold as dotted... plt.plot(np.ma.masked_where(below,x),np.ma.masked_where(below,y1),'b--') plt.plot(np.ma.masked_where(below,y2),'g--') # Plot lines above threshold as solid... plt.plot(np.ma.masked_where(above,np.ma.masked_where(above,'b-') plt.plot(np.ma.masked_where(above,'g-') plt.show()