使用Matlotlib LineCollection的列表格式错误

时间:2019-03-18 17:57:42

标签: python-2.7 matplotlib

我有一个列表(coordpairs)试图用作使用LineCollection进行绘图的基础。该列表来自熊猫数据框。尽管公认有明显的错误代码,但我无法以正确的格式获取列表。整理后的数据框内容,代码和错误如下。谢谢您的帮助。

数据框架的一部分

RUP_ID  Vert_ID Longitude   Latitude
1   1   -116.316961 34.750178
1   2   -116.316819 34.750006
2   1   -116.316752 34.749938
2   2   -116.31662  34.749787
10  1   -116.317165 34.754078
10  2   -116.317277 34.751492
10  3   -116.317206 34.751273
10  4   -116.317009 34.75074
10  5   -116.316799 34.750489
11  1   -116.316044 34.760377
11  2   -116.317105 34.755674
11  3   -116.317165 34.754078

代码

import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
fig = plt.figure()
ax1 = plt.subplot2grid((2, 2), (0, 0), rowspan=2, colspan=1)
for ii in range(1,len(mydf)):
    temp = mydf.loc[mydf.RUP_ID == ii]
        df_line = temp.sort_values(by='Vert_ID', ascending=True)
        del temp
        lat = df_line.Latitude
        lon = df_line.Longitude
        lat = lat.tolist()
    long = long.tolist()
    coordpairs = zip(lat, long)
    lc = LineCollection(coordpairs, colors='r') # this is line 112 in the error
    ax1.add_collection(lc)

# note I also tried:
# import numpy as np
# coordpairs2 = np.vstack([np.array(u) for u in set([tuple(p) for p in coordpairs])])
# lc = LineCollection(coordpairs2, colors='r')
# and received the same plotting error

错误/输出

C:\apath\python.exe C:/mypath/myscript.py
Traceback (most recent call last):
  File "C:/mypath/myscript.py", line 112, in <module>
    lc = LineCollection(coordpairs, colors='r')  # this is line 112 in the error
  File "C:\apath\lib\site-packages\matplotlib\collections.py", line 1149, in __init__
    self.set_segments(segments)
  File "C:\apath\lib\site-packages\matplotlib\collections.py", line 1164, in set_segments
    self._paths = [mpath.Path(_seg) for _seg in _segments]
  File "C:\apath\lib\site-packages\matplotlib\path.py", line 141, in __init__
    raise ValueError(msg)
ValueError: 'vertices' must be a 2D list or array with shape Nx2

Process finished with exit code 1

1 个答案:

答案 0 :(得分:0)

您可能要创建一个包含多行的单个LineCollection,第一行数据框列中的每个RUP_ID值。这意味着最好循环遍历该列的唯一值(而不是遍历每一行!),并将坐标附加到列表中。使用该列表作为LineCollection的输入。

u = """RUP_ID  Vert_ID Longitude   Latitude
1   1   -116.316961 34.750178
1   2   -116.316819 34.750006
2   1   -116.316752 34.749938
2   2   -116.31662  34.749787
10  1   -116.317165 34.754078
10  2   -116.317277 34.751492
10  3   -116.317206 34.751273
10  4   -116.317009 34.75074
10  5   -116.316799 34.750489
11  1   -116.316044 34.760377
11  2   -116.317105 34.755674
11  3   -116.317165 34.754078"""

import io
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

df = pd.read_csv(io.StringIO(u), sep="\s+")

verts = []
for (RUP_ID, grp) in df.groupby("RUP_ID"):

    df_line = grp.sort_values(by='Vert_ID', ascending=True)

    lat = df_line.Latitude
    lon = df_line.Longitude

    verts.append(list(zip(lon, lat)))

lc = LineCollection(verts, color='r')

fig, ax = plt.subplots()
ax.add_collection(lc)
ax.autoscale()
plt.show()