Numpy 如何使用matplotlib编程取决于节距的彩色线段

Numpy 如何使用matplotlib编程取决于节距的彩色线段,numpy,matplotlib,Numpy,Matplotlib,我正在打印一个图表,其中一条线来自两个numpy数组,其中的浮点数相同,就像这样,它工作正常 f_used = sp.interpolate.interp1d(time, distance, kind='cubic') timeinterp = sp.arange(0, runtime+incr, incr) distinterp = f_used(timeinterp) plt.plot(timeinterp, distinterp, '-', lw=3, c="red" ) 到目前为止,一

我正在打印一个图表,其中一条线来自两个numpy数组,其中的浮点数相同,就像这样,它工作正常

f_used = sp.interpolate.interp1d(time, distance, kind='cubic')
timeinterp = sp.arange(0, runtime+incr, incr)
distinterp = f_used(timeinterp)
plt.plot(timeinterp, distinterp, '-', lw=3, c="red" )
到目前为止,一切顺利。在下一步中,我想根据音高绘制线段(
distinterp/timeinterp
)。如果比率大于5.0,那么让我们说线条样式应该是“虚线”或/和获得另一种颜色。 我找不到任何解决办法。有人有主意吗


如果有帮助:Raspbian on Raspberry Pi 3,所有软件更新,使用Python3

您将不得不有效地将数据分割成您想要的不同部分,因为每个线条对象只能有一个样式/颜色/等组合指定给它

使用numpy(或scipy,在您的情况下,scipy只是直接导入底层numpy函数)应该很简单:

更好的方法可能是使用matplotlib的面向对象API:

mask = (distinterp / timeinterp) > 5.0
fig, ax = plt.subplots()
ax.plot(timeinterp[mask], distinterp[mask], ':', lw=3, c='r')
ax.plot(timeinterp[~mask], distinterp[~mask], '-', lw=3, c='b')

请看一下我是如何编辑您的问题的,并在将来以相同的方式(缩进4个空格)格式化您的代码块的。回答得好,但是你介意删去那些毫无意义的陈述吗。后续调用
plot
将始终绘制到相同的轴,并且
plt.hold
的使用被降低。非常感谢您的快速回答。似乎有效。现在我可以继续工作了。
mask = (distinterp / timeinterp) > 5.0
fig, ax = plt.subplots()
ax.plot(timeinterp[mask], distinterp[mask], ':', lw=3, c='r')
ax.plot(timeinterp[~mask], distinterp[~mask], '-', lw=3, c='b')