1

Here is my y values:

[1.00, 
[(1.99-2.27e-17j),(0.61+9.08e-17j), (0.12-0j)], 
[(1.9+4.54e-17j), (0.61-9.081e-17j), (0.12+4.54e-17j)], 
[(1.99+4.5e-17j), (0.61-9.08e-17j), (0.12+4.54e-17j)], 
[(1.99-2.27e-17j), (0.61+9.0e-17j), (0.12-0j)], 
3.00]

And here is my x values:

array([ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ])

As you can see, the first and last x values correspond to one y value , the rest of the x values correspond to multiple y values. So how to plot the real part of y values against x ?

1

1 Answer 1

2

Here is a solution that might help you. Note that this isn't really efficient (especially a problem for bigger datasets), but might put you on the right track. The main issue is that the dimensionality between your xs and ys is not the same.

from numpy import *
from matplotlib import pyplot as plt

ys = [1.00, 
[(1.99-2.27e-17j),(0.61+9.08e-17j), (0.12-0j)], 
[(1.9+4.54e-17j), (0.61-9.081e-17j), (0.12+4.54e-17j)], 
[(1.99+4.5e-17j), (0.61-9.08e-17j), (0.12+4.54e-17j)], 
[(1.99-2.27e-17j), (0.61+9.0e-17j), (0.12-0j)], 
3.00]

xs = array([ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ])

pxs = []
pys = []
for i, yelems in enumerate(ys):
    x = xs[i]
    try:
        for yelem in yelems:
            pxs.append(x)
            pys.append(yelem.real)
    except TypeError:
        pxs.append(x)
        pys.append(yelems)

plt.plot(pxs, pys)
plt.show()

As you can see I simply create two (one-dimensional) lists where I re-use the respective x value as needed so that pxs and pys have the same length. The TypeError is raised when trying to iterate over a float in the inner for loop, i.e. when yelems is only one element instead of a list.

I also assumed that you only want the real part of the yelems if there are multiple values (yelems is a list rather than a float) for one x.

Update:

Regarding your question below whether the data can be "plotted directly", I have never used data like that with Matplotlib where the y dimensions vary across the x range.

Depending on what you want to achieve, you might be able to clean up the code by being smart about the ys entries (i.e. make the ones at the end lists rather than floats for consistency) and/or use list comprehensions and/or tricks with zip for a more "compact" feel where you call plot.

If at all possible, personally I would try and get the input data straight as early on as possible to avoid the need for such re-arrangements of the entries.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. I have use the spirit of your method to rewrite my code and it works. But is it possible for python to directly plot x:[1,2,3] y:[1, [1,2,3], 3]?
You're welcome. See my update regarding your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.