2016-07-15 73 views
3

如果我绘制各种不相交的线,一个电话如下...蟒蛇多行作为一组

>>> import matplotlib.pyplot as plt 
>>> x = [random.randint(0,9) for i in range(10)] 
>>> y = [random.randint(0,9) for i in range(10)] 
>>> data = [] 
>>> for i in range(0,10,2): 
...  data.append((x[i], x[i+1])) 
...  data.append((y[i], y[i+1])) 
... 
>>> print(data) 
[(6, 4), (4, 3), (6, 5), (0, 4), (0, 0), (2, 2), (2, 0), (6, 5), (2, 5), (3, 6)] 
>>> plt.plot(*data) 
[<matplotlib.lines.Line2D object at 0x0000022A20046E48>, <matplotlib.lines.Line2D object at 0x0000022A2004D048>, <matplotlib.lines.Line2D object at 0x0000022A2004D9B0>, <matplotlib.lines.Line2D object at 0x0000022A20053208>, <matplotlib.lines.Line2D object at 0x0000022A20053A20>] 
>>> plt.show() 

enter image description here

我无法弄清楚如何我得到的Python/matplotlib把它看作一个单一的情节,相同的颜色,线宽,ECT和相同的图例项...

预先感谢您

回答

1

如果你不介意他们都被合并成一个线比你只需要使用plt.plot(x,y)。不过,我认为你想把它们作为单独的行。为此,您可以指定样式参数到您的绘图文件,然后使用Stop matplotlib repeating labels in legend中的代码来防止多个图例条目。

import matplotlib.pyplot as plt 
import numpy as np 
from collections import OrderedDict 

x = [np.random.randint(0,9) for i in range(10)] 
y = [np.random.randint(0,9) for i in range(10)] 
data = [] 
for i in range(0,10,2): 
    data.append((x[i], x[i+1])) 
    data.append((y[i], y[i+1])) 

#Plot all with same style and label. 
plt.plot(*data,linestyle='-',color='blue',label='LABEL') 

#Single Legend Label 
handles, labels = plt.gca().get_legend_handles_labels() 
by_label = OrderedDict(zip(labels, handles)) 
plt.legend(by_label.values(), by_label.keys()) 

#Show Plot 
plt.show() 

给你
enter image description here

0

这个怎么样的?

import numpy as np 
d = np.asarray(data) 
plt.plot(d[:,0],d[:,1]) 
plt.show()