Question

Hi I have a 3D list (I realise this may not be the best representation of my data so any advice here is appreciated) as such:

y_data = [ 
         [[a,0],[b,1],[c,None],[d,6],[e,7]],
         [[a,5],[b,2],[c,1],[d,None],[e,1]],
         [[a,3],[b,None],[c,4],[d,9],[e,None]],
         ]

The y-axis data is such that each sublist is a list of values for one hour. The hours are the x-axis data. Each sublist of this has the following format: [label,value]

So essentially:

line a is [0,5,3] on the y-axis
line b is [1,2,None] on the y-axis etc.

My x-data is:

x_data = [0,1,2,3,4]

Now when I plot this list directly i.e.

for i in range(0,5):
    ax.plot(x_data, [row[i][1] for row in y_data], label=y_data[0][i][0])

I get a line graph however where the value is None the point is not drawn and the line not connected.

What I would like to do is to have a graph which will plot my data in it's current format, but ignore missing points and draw a line between the point before the missing data and the point after (i.e. interpolating the missing point).

I tried doing it like this https://stackoverflow.com/a/14399830/1800665 but I couldn't work out how to do this for a 3D list.

Thanks for any help!

Was it helpful?

Solution

The general approach that you linked to will work fine here ; it looks like the question you're asking is how to apply that approach to your data. I'd like to suggest that by factoring out the data you're plotting, you'll see more clearly how to do it.

import numpy as np

y_data = [ 
     [[a,0],[b,1],[c,None],[d,6],[e,7]],
     [[a,5],[b,2],[c,1],[d,None],[e,1]],
     [[a,3],[b,None],[c,4],[d,9],[e,None]],
     ]

x_data = [0, 1, 2, 3, 4]

for i in range(5):
    xv = []
    yv = []
    for j, v in enumerate(row[i][1] for row in y_data):
        if v is not None:
            xv.append(j)
            yv.append(v)
    ax.plot(xv, yv, label=y_data[0][i][0])

Here instead of using a mask like in the linked question/answer, I've explicitly built up the lists of valid data points that are to be plotted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top