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.