python将多行作为一个组

python将多行作为一个组,python,matplotlib,plot,Python,Matplotlib,Plot,如果我用一个调用绘制各种不相交的线,如下所示 >>> 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): ...

如果我用一个调用绘制各种不相交的线,如下所示

>>> 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()
>>将matplotlib.pyplot作为plt导入
>>>x=[random.randint(0,9)表示范围(10)内的i]
>>>y=[random.randint(0,9)表示范围(10)内的i]
>>>数据=[]
>>>对于范围(0,10,2)内的i:
...     data.append((x[i],x[i+1]))
...     data.append((y[i],y[i+1]))
... 
>>>打印(数据)
[(6, 4), (4, 3), (6, 5), (0, 4), (0, 0), (2, 2), (2, 0), (6, 5), (2, 5), (3, 6)]
>>>plt.绘图(*数据)
[, , ]
>>>plt.show()

我不知道如何让python/matplotlib将其视为一个单一的绘图,具有相同的颜色、线宽等和相同的图例条目

提前谢谢你

这个怎么样

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

如果您不介意将它们全部合并到一行中,那么只需使用
plt.plot(x,y)
。然而,我想你会把它们作为单独的行。为此,您可以指定绘图comamnd的样式参数,然后使用中的代码来防止多个图例条目

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()
给你

我认为这不起作用,因为它连接不同线路的端点