arrays - plot multiple y values against one x values in python -
here 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 x values:
array([ 0. , 0.2, 0.4, 0.6, 0.8, 1. ])
as can see, first , last x values correspond 1 y value , rest of x values correspond multiple y values. how plot real part of y values against x ?
here solution might you. note isn't efficient (especially problem bigger datasets), might put on right track. main issue dimensionality between xs
, ys
not same.
from numpy import * matplotlib import pyplot 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 = [] i, yelems in enumerate(ys): x = xs[i] try: 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 can see create 2 (one-dimensional) lists re-use respective x
value needed pxs
, pys
have same length. typeerror
raised when trying iterate on float in inner for
loop, i.e. when yelems
1 element instead of list.
i assumed want real
part of yelems
if there multiple values (yelems
list rather float) 1 x
.
update:
regarding question below whether data can "plotted directly", have never used data matplotlib y dimensions vary across x range.
depending on want achieve, might able clean code being smart ys
entries (i.e. make ones @ end lists rather floats consistency) and/or use list comprehensions and/or tricks zip
more "compact" feel call plot
.
if @ possible, try , input data straight on possible avoid need such re-arrangements of entries.
Comments
Post a Comment