2016-11-19 88 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

相关问题