Home > Software design >  Separate lists in nested list, and plotting first and second row values
Separate lists in nested list, and plotting first and second row values

Time:05-29

I have a longer version of the list seen below, consisting of coordinates to different polygons (3 polygons in list below, 2000 in my actual list).

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]]]

I want to plot the first row values in the list in MainList as x, and second row as y. Therefore I tried to flatlist 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]

when plotting this, plt.plot(x_TEST,y_TEST): 3 polygons attaching lines

The problem is, there is a line connecting each polygon to each other.

Edit: I also tried without flatlisting it, but that would only plot the first polygon

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

How do I plot all 3 polygons, it without the line, each polygon separated?

CodePudding user response:

The code below should work nicely.

You do not need to flatten the list completely as in this case you want to maintain the x,y columns for the plot. So to separate the columns with a list comprehension on the nested MainList works and then you plot it.

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")

A more generalised method that helps if you have a bigger list:

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)

  • Related