编辑:我已经这样开放了,所以我没有注意到里卡多的回答.因为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