2017-02-13 91 views
1

我打印的內容從與相同數量的花車他們這樣的2個numpy的陣列來了一個線圖和它的正常工作程序。如何根據音調彩色線條部分與matplotlib

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上樹莓派3,所有的軟件更新,使用Python3

+0

請看看我如何編輯您的問題,並在將來以相同的方式(4空格縮進)格式化您的代碼塊。使眼睛更輕鬆。 –

回答

1

您將有效地必須將數據轉換成你想要,因爲每行對象只能有一個風格不同的部分開/顏色/等等。分配給它的組合。

這應該使用numpy的(或SciPy的,而你的情況僅僅是直接進口的底層numpy的功能)是微不足道的:

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

一個更好的辦法可能是使用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') 
+0

好的答案,但你會介意刪除關於['plt.hold(True)'](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hold)的聲明,這是沒有任何意義的。 plot'的'後續調用將始終繪製到同一座標和使用plt.hold'的'折舊。 – ImportanceOfBeingErnest

+0

非常感謝這個快速的答案。似乎工作。現在我可以繼續工作了。 –

+0

@ImportanceOfBeingErnest。你是對的。固定。 –