在嵌套列表中分隔列表,并绘制第一行和第二行值
Separate lists in nested list, and plotting first and second row values
我有下面列表的更长版本,由不同多边形的坐标组成(下面列表中有 3 个多边形,我的实际列表中有 2000 个)。
MainList = [[[-126.38493347032566, 8.065018490287665],
[-126.37640789246038, 7.9425580414237755],
[-126.29555487198307, 7.957675770047483],
[-126.09180970014715, 7.757175961695489],
[-126.24982282607408, 7.561646379709267],
[-126.7162036678627, 7.309702635668215],
[-126.85617174950822, 7.074462402268876],
[-127.09392505521747, 7.227355837187626],
[-127.13986378496843, 7.6147958544569665],
[-127.29888183431987, 7.854024068113819],
[-127.10320127147244, 8.123400890876232],
[-127.18824938216802, 8.371078735837333],
[-127.08770136961274, 8.394624626355522],
[-126.51294868490649, 8.170702350367074],
[-126.38493347032566, 8.065018490287665]],
[[-127.82582769243184, 8.649146321871195],
[-127.82674891366018, 8.59343262850995],
[-127.97373872285344, 8.596277618944901],
[-127.82582769243184, 8.649146321871195]],
[[-128.12402462883344, 8.815270505675027],
[-128.256760067403, 8.7946653101063],
[-128.3467261891677, 8.940798405802703],
[-128.25063346232488, 8.970663263189133],
[-128.10796189381801, 8.891567942585386],
[-128.12402462883344, 8.815270505675027]]]
我想将 MainList
列表中的第一行值绘制为 x
,将第二行绘制为 y
。
因此我尝试将 MainList
flatlistTEST = [el for lst1 in MainList for lst2 in lst1 for el in lst2]
x_TEST, y_TEST = flatlistTEST[0::2],flatlistTEST[1::2]
绘制此图时,plt.plot(x_TEST,y_TEST)
:
问题是,有一条线将每个多边形相互连接起来。
编辑:
我也尝试过不将其列为扁平化,但这只会绘制第一个多边形
x_test = [line[0] for line in MainList[0]]
y_test = [line[1] for line in MainList[0]]
plt.plot(x_test,y_test)
Only 1 polygon plotted
如何绘制所有 3 个多边形,它没有线,每个多边形分开?
下面的代码应该可以很好地工作。
您不需要完全展平列表,因为在这种情况下您想要保留绘图的 x,y 列。因此,在嵌套的 MainList 上使用列表理解来分隔列,然后绘制它。
import matplotlib.pyplot as plt
MainList = [[[-126.38493347032566, 8.065018490287665], [-126.37640789246038, 7.9425580414237755], [-126.29555487198307, 7.957675770047483], [-126.09180970014715, 7.757175961695489], [-126.24982282607408, 7.561646379709267], [-126.7162036678627, 7.309702635668215], [-126.85617174950822, 7.074462402268876], [-127.09392505521747, 7.227355837187626], [-127.13986378496843, 7.6147958544569665], [-127.29888183431987, 7.854024068113819], [-127.10320127147244, 8.123400890876232], [-127.18824938216802, 8.371078735837333], [-127.08770136961274, 8.394624626355522], [-126.51294868490649, 8.170702350367074], [-126.38493347032566, 8.065018490287665]],[[-127.82582769243184, 8.649146321871195], [-127.82674891366018, 8.59343262850995], [-127.97373872285344, 8.596277618944901], [-127.82582769243184, 8.649146321871195]],[[-128.12402462883344, 8.815270505675027], [-128.256760067403, 8.7946653101063], [-128.3467261891677, 8.940798405802703], [-128.25063346232488, 8.970663263189133], [-128.10796189381801, 8.891567942585386], [-128.12402462883344, 8.815270505675027]]]
x1_test = [line[0] for line in MainList[0]]
y1_test = [line[1] for line in MainList[0]]
x2_test = [line[0] for line in MainList[1]]
y2_test = [line[1] for line in MainList[1]]
x3_test = [line[0] for line in MainList[2]]
y3_test = [line[1] for line in MainList[2]]
plt.plot(x1_test, y1_test, color="green", label="first")
plt.plot(x2_test, y2_test, color="blue", label="second")
plt.plot(x3_test, y3_test, color="red", label="third")
如果您有更大的列表,一种更通用的方法会有所帮助:
import matplotlib.pyplot as plt
def polygons(main_list, list_index, color="black"):
x1_test = [line[0] for line in main_list[list_index]]
y1_test = [line[1] for line in main_list[list_index]]
plt.plot(x1_test, y1_test, color=color)
for list_index in range(len(MainList)):
polygons(main_list=MainList, list_index=list_index)
我有下面列表的更长版本,由不同多边形的坐标组成(下面列表中有 3 个多边形,我的实际列表中有 2000 个)。
MainList = [[[-126.38493347032566, 8.065018490287665],
[-126.37640789246038, 7.9425580414237755],
[-126.29555487198307, 7.957675770047483],
[-126.09180970014715, 7.757175961695489],
[-126.24982282607408, 7.561646379709267],
[-126.7162036678627, 7.309702635668215],
[-126.85617174950822, 7.074462402268876],
[-127.09392505521747, 7.227355837187626],
[-127.13986378496843, 7.6147958544569665],
[-127.29888183431987, 7.854024068113819],
[-127.10320127147244, 8.123400890876232],
[-127.18824938216802, 8.371078735837333],
[-127.08770136961274, 8.394624626355522],
[-126.51294868490649, 8.170702350367074],
[-126.38493347032566, 8.065018490287665]],
[[-127.82582769243184, 8.649146321871195],
[-127.82674891366018, 8.59343262850995],
[-127.97373872285344, 8.596277618944901],
[-127.82582769243184, 8.649146321871195]],
[[-128.12402462883344, 8.815270505675027],
[-128.256760067403, 8.7946653101063],
[-128.3467261891677, 8.940798405802703],
[-128.25063346232488, 8.970663263189133],
[-128.10796189381801, 8.891567942585386],
[-128.12402462883344, 8.815270505675027]]]
我想将 MainList
列表中的第一行值绘制为 x
,将第二行绘制为 y
。
因此我尝试将 MainList
flatlistTEST = [el for lst1 in MainList for lst2 in lst1 for el in lst2]
x_TEST, y_TEST = flatlistTEST[0::2],flatlistTEST[1::2]
绘制此图时,plt.plot(x_TEST,y_TEST)
:
问题是,有一条线将每个多边形相互连接起来。
编辑: 我也尝试过不将其列为扁平化,但这只会绘制第一个多边形
x_test = [line[0] for line in MainList[0]]
y_test = [line[1] for line in MainList[0]]
plt.plot(x_test,y_test)
Only 1 polygon plotted
如何绘制所有 3 个多边形,它没有线,每个多边形分开?
下面的代码应该可以很好地工作。
您不需要完全展平列表,因为在这种情况下您想要保留绘图的 x,y 列。因此,在嵌套的 MainList 上使用列表理解来分隔列,然后绘制它。
import matplotlib.pyplot as plt
MainList = [[[-126.38493347032566, 8.065018490287665], [-126.37640789246038, 7.9425580414237755], [-126.29555487198307, 7.957675770047483], [-126.09180970014715, 7.757175961695489], [-126.24982282607408, 7.561646379709267], [-126.7162036678627, 7.309702635668215], [-126.85617174950822, 7.074462402268876], [-127.09392505521747, 7.227355837187626], [-127.13986378496843, 7.6147958544569665], [-127.29888183431987, 7.854024068113819], [-127.10320127147244, 8.123400890876232], [-127.18824938216802, 8.371078735837333], [-127.08770136961274, 8.394624626355522], [-126.51294868490649, 8.170702350367074], [-126.38493347032566, 8.065018490287665]],[[-127.82582769243184, 8.649146321871195], [-127.82674891366018, 8.59343262850995], [-127.97373872285344, 8.596277618944901], [-127.82582769243184, 8.649146321871195]],[[-128.12402462883344, 8.815270505675027], [-128.256760067403, 8.7946653101063], [-128.3467261891677, 8.940798405802703], [-128.25063346232488, 8.970663263189133], [-128.10796189381801, 8.891567942585386], [-128.12402462883344, 8.815270505675027]]]
x1_test = [line[0] for line in MainList[0]]
y1_test = [line[1] for line in MainList[0]]
x2_test = [line[0] for line in MainList[1]]
y2_test = [line[1] for line in MainList[1]]
x3_test = [line[0] for line in MainList[2]]
y3_test = [line[1] for line in MainList[2]]
plt.plot(x1_test, y1_test, color="green", label="first")
plt.plot(x2_test, y2_test, color="blue", label="second")
plt.plot(x3_test, y3_test, color="red", label="third")
如果您有更大的列表,一种更通用的方法会有所帮助:
import matplotlib.pyplot as plt
def polygons(main_list, list_index, color="black"):
x1_test = [line[0] for line in main_list[list_index]]
y1_test = [line[1] for line in main_list[list_index]]
plt.plot(x1_test, y1_test, color=color)
for list_index in range(len(MainList)):
polygons(main_list=MainList, list_index=list_index)