2016-07-15 76 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()