2016-11-19 4 views
0

インデックスを参照して点が色付けされている変数をプロットすることができません。私が最終的に望むのは、(次の点に接続する)各点の線分を特定の色にすることです。私はMatplotlibpandasの両方で試しました。各メソッドは異なるエラーをスローします。Pythonで複数色の線をプロットしようとするとエラーが発生する

トレンドラインの生成:

  0 labels 
0 0.000000  2 
1 0.598472  2 
2 -0.958924  1 
3 0.938000  2 
4 -0.544021  1 
:私たちの最終的なデータセットを生成する

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

:だから今

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

plt.plot(sinned) 

edgy sin graph

を、私たちは、ラベルの新しい列を生成します

そして今、プロットの試みのために:エラーになり

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

length of rgba sequence should be either 3 or 4

私も:-/

+1

[パイソン:どのように異なる色で1本のラインをプロットする]の可能な重複(http://stackoverflow.com/questions/17240694/python-how-to-plot-one-line -in-different-colours) – ImportanceOfBeingErnest

+0

この質問を見る:http://stackoverflow.com/questions/17240694/python-how-to-plot-one-line-in-different-colorsまた、matplotlib [example色付けラインについて](http://matplotlib.org/examples/pylab_examples/multicolored_line.html) – ImportanceOfBeingErnest

+0

@ImportanceOfBeingErnest今、あなたが今提案した質問を通過するだけです。 –

答えて

1
いずれも動作しませんでした、文字列 'r'または 'b'するラベルを設定してみました

1と2は色ではなく、'b' lueと'r'が以下の例で使用されています。それぞれ別々にプロットする必要があります。

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

関連する問題