0

I am struggling to plot correctly a list that contains some nested list. This is actually the output results of my calculation, and they are calculated based on my input data. So, I would like to plot the out put based on pair of coordinates. I have the sample data as following:

temp = [(8.30014492234868, 10.076987589320124), 
        (6.565100209934528, 10.126390914006539), 
        (8.064572331389588, 9.999998485114327), 
        (18.801449684428746, 1.1966541400666473), 
        (16.39706266718748, -0.07867904744412477),
        (13.489350431455016, -0.6138733861514962),
        (13.668995714960388, 17.53643661173564), 
        (4.808988910967888, 20.76029261433188), 
        (1.7575932694038692, 14.404108129869353), 
        (3.0136007564635103, 7.318480031628899), 
        (2.988551059917197, 7.355468722551662), 
        (18.826817655400742, 0.30186049637530527),
        (4.9477946187407476, 16.065642524553212), 
        (5.7416001839901165, 5.915851736013389),
        (3.004349636718835, 7.315738092232703),
        ((1.481287160721499, 13.69706806608235), (4.975290244180029, 15.059417315522877)), 
        (9.022336802561307, 8.741822450784232), 
        (8.339683645685863, 9.875063384164433),
        (13.625980549650077, 17.64071625638145), 
        (7.6474833153988016, 20.09172492469006), 
        (9.777767270079616, 1.7478894865558094)]

As you can see all of them are pairs and can be easily plotted except the one looks like this one: ((1.481287160721499, 13.69706806608235), (4.975290244180029, 15.059417315522877)).

Sometimes I am getting many of them, and I am not able to plot them correctly. This is what I used:

for x, y in temp:
    plt.scatter(x, y, c='k', marker='o', s=4)

for line in temp:
    xll,yll = line.xy
    plt.scatter(xll, yll, color='c', lw=1, zorder=0)

but none of them worked.

Georgy
  • 12,464
  • 7
  • 65
  • 73
ash
  • 55
  • 5
  • Does this answer your question? [How to flatten a list of nested tuples in Python?](https://stackoverflow.com/questions/47431752/how-to-flatten-a-list-of-nested-tuples-in-python) After flattening the list, you can use your first code to plot it. – Georgy Mar 02 '20 at 09:00
  • I did, but all get flatten except the one has two round bracket – ash Mar 02 '20 at 15:10
  • 1
    When I try `result = [z for y in (x if isinstance(x[0],tuple) else [x] for x in temp) for z in y]` from the first answer, it produces the expected result. Can you try again? – Georgy Mar 02 '20 at 15:41

0 Answers0