1

My question is related to this one "Python: plot list of tuples". The difference lies in the structure of the tuple. The first part shows the number of the plot and the second the value (y). I have managed to extract the data from one big list into three lists.

How can I plot this tuple-list in three plots (line plot)?

[(1, '0'), (1, '4'), (1, '2'), (1, '6'), (1, '3'), (1, '4'), (1, '5'), (1, '6'), (1, '7'), (1, '12'), (1, '23')]
[(2, '1'), (2, '7'), (2, '2'), (2, '4'), (2, '1'), (2, '7'), (2, '2'), (2, '3'), (2, '4'), (2, '2'), (2, '3')]
[(3, '2'), (3, '4'), (3, '4'), (3, '2'), (3, '1'), (3, '13'), (3, '9'), (3, '8'), (3, '4'), (3, '2'), (3, '7')]
Plato77
  • 422
  • 3
  • 15
Stani
  • 35
  • 5
  • Please clarify what your desired outcome is. Otherwise the answer will simply be: "You cannot." Because: If the first element of each tuple is the number of the plot, then there is only element left in each tuple. And you cannot plot a 2D line from 1D data. – Thomas Jan 06 '20 at 18:38
  • The second value is on the (y) axis and for the (x) it counts simply up by 1 per new tuple. (New plot starting again from 0 for x) – Stani Jan 07 '20 at 08:41

2 Answers2

0

It's the same, just add a function to clean the tuples to convert the strings to ints:

import matplotlib.pyplot as plt

p1 = [(1, '0'), (1, '4'), (1, '2'), (1, '6'), (1, '3'), (1, '4'), (1, '5'), (1, '6'), (1, '7'), (1, '12'), (1, '23')]
p2 = [(2, '1'), (2, '7'), (2, '2'), (2, '4'), (2, '1'), (2, '7'), (2, '2'), (2, '3'), (2, '4'), (2, '2'), (2, '3')]
p3 = [(3, '2'), (3, '4'), (3, '4'), (3, '2'), (3, '1'), (3, '13'), (3, '9'), (3, '8'), (3, '4'), (3, '2'), (3, '7')]

def cleaner(mylist):
    out = []
    for tup in mylist:
        newtup = tuple()
        for item in tup:
            newtup += (int(item),)
        out.append(newtup)
    return(out)

plt.plot(*zip(*cleaner(p1)))
plt.show()
tbrk
  • 156
  • 8
0

You have to create a 3rd dimension to your tuple to represent the x axis, otherwise there's nothing to plot. I'm assuming these are in order and that the index of the tuple within the list can be treated as a sort of time element.

I think it would be easier to understand if you take it step by step. You can treat the first item in each tuple as the line color and the second as the y-axis value. Assuming these are in order, you can enumerate each list and take the incrementing value as your x-axis value.

So iterate over all 3 lists, then iterate over each enumerated tuple, appending to a master list data what will be the x, y, and color values for your final data set.

Pull those into a pandas dataframe and use seaborn to cleanly create the multi line plot that you are looking for.

import pandas as pd
import seaborn as sns

a = [(1, '0'), (1, '4'), (1, '2'), (1, '6'), (1, '3'), (1, '4'), (1, '5'), (1, '6'), (1, '7'), (1, '12'), (1, '23')]
b = [(2, '1'), (2, '7'), (2, '2'), (2, '4'), (2, '1'), (2, '7'), (2, '2'), (2, '3'), (2, '4'), (2, '2'), (2, '3')]
c = [(3, '2'), (3, '4'), (3, '4'), (3, '2'), (3, '1'), (3, '13'), (3, '9'), (3, '8'), (3, '4'), (3, '2'), (3, '7')]

data = []
for x in [a,b,c]:
    for i, t in enumerate(x):
        data.append([i, t[0], t[1]])

df = pd.DataFrame(data)
df = df.astype(int)
df.columns=['Time','Color','Value']

sns.lineplot(x='Time',y='Value',hue='Color',data=df)
Chris
  • 15,819
  • 3
  • 24
  • 37
  • I used a combination of the answers like that: `data = [] for x in [a]: for i, t in enumerate(x): data.append([i, int(t[1])]) plt.plot(*zip(*cleaner(data))) plt.show()` – Stani Jan 07 '20 at 09:20