2016-11-19 44 views
0

我無法繪製一個變量,其中的點通過引用而被着色。我最終想要的是每個點的線段(連接到下一個點)是一種特定的顏色。我嘗試了Matplotlibpandas。每種方法都會引發不同的錯誤。嘗試在Python中繪製多色線時發生錯誤

生成趨勢線:

datums = np.linspace(0,10,5) 
sinned = np.sin(datums) 

plt.plot(sinned) 

edgy sin graph

所以現在我們產生了一個新的標籤欄:

sinned['labels'] = np.where((sinned < 0), 1, 2) 
print(sinned) 

能產生我們的最終數據集:

  0 labels 
0 0.000000  2 
1 0.598472  2 
2 -0.958924  1 
3 0.938000  2 
4 -0.544021  1 

現在的陰謀企圖:

plt.plot(sinned[0], c = sinned['labels']) 

這會導致錯誤:length of rgba sequence should be either 3 or 4

我也嘗試設置標籤是字符串'r''b',這並沒有工作,要麼:-/

+1

的可能的複製[蟒:如何繪製不同的顏色一行](http://stackoverflow.com/questions/17240694/python-how-to-plot-one-line在不同的顏色) – ImportanceOfBeingErnest

+0

看看這個問題:http://stackoverflow.com/questions/17240694/python-how-to-plot-one-line-in-different-colors此外,還有一個matplotlib [示例關於着色行](http://matplotlib.org/examples/pylab_examples/multicolored_line.html) – ImportanceOfBeingErnest

+0

@ImportanceOfBeingErnest我只是通過你現在建議的問題。 –

回答

1

1和2不是顏色,'b' lue和'r' ed在下面的示例中使用。你需要分別繪製每一個。

import matplotlib.pyplot as plt 
import numpy as np 
import pandas as pd 

datums = np.linspace(0,10,5) 

sinned = pd.DataFrame(data=np.sin(datums)) 
sinned['labels'] = np.where((sinned < 0), 'b', 'r') 
fig, ax = plt.subplots() 

for s in range(0, len(sinned[0]) - 1): 
    x=(sinned.index[s], sinned.index[s + 1]) 
    y=(sinned[0][s], sinned[0][s + 1]) 
    ax.plot(x, y, c=sinned['labels'][s]) 
plt.show() 

Output

相關問題