# Strange list_plot artifact?

I am creating and using a list of complex numbers, and I want to plot them on the plane. This has so far been simple. However, when using two different codes to do the same thing, I'm coming up with two different pictures. Unfortunately, the simpler code is giving me the incorrect graphic.

I have my list roots, and it has all my points in a list of lists. (I prefer to keep these lists separate as they denote different types of points I'm looking at.)

When I used the code:

allroots=[]
for period in roots:
for i in period:
allroots.append(i)
pic=list_plot([m for m in allroots])


my graphics come out exactly as desired. However, when I use the following code:

plot = sum(list_plot(i) for i in roots)


which is clearly the simpler code, the picture has changed to show points that are not in my list. For example, my list does not have the point 1, (which I have checked multiple times because that would break math) and yet this simpler code consistently plots a point at 1, as well as a few other seemingly random points.

Can anyone explain what's happening or give a better code to use? I'd really like to plot my list of lists without having to compile them into a single list, but this code is causing me problems, and I have no idea why.

edit retag close merge delete

Sort by » oldest newest most voted

use point2d to plot a single point, not list_plot

more

Thank you! I'm not sure why this fixed it, but it did!

( 2021-03-23 20:28:22 +0200 )edit

Compare the output of

list_plot([-1, 1, -1+I, -1-I], size=80)


with that of

list_plot([-1, 1], size=80) + list_plot([-1+I, -1-I], size=80)


In the fisrt case, you see four points located at $(-1,0)$, $(1,0)$, $(-1,1)$ and $(-1,-1)$. This is the correct way to place $-1$, $1$, $-1+I$ and $-1-I$ in the complex plane. However, in the second case, you get four points located at $(0,-1)$, $(1,1)$, $(-1,1)$ and $(-1,-1)$, which is not what you can expect.

The problem comes from the way list_plot interprets its argument. If the list contains a complex number, list_plot thinks that every element $z$ of the list is a complex number and places it at $(x,y)$, $x$ and $y$ being the real and imaginary parts of $z$. If the list only contains real numbers, say, $x_0, x_1, x_2\ldots$, list_plot places them at $(0,x_0)$, $(1,x_1)$, $(2,x_2)$ and so on.

I conjecture that some list in roots only contains real numbers. So some list_plot in the code

sum(list_plot(i) for i in roots)


may misinterpret its argument and draw points at undesired locations. However, when you unify all the roots in a single list, since it contains at least one complex number, list_plot behaves as expected. By the way, you can construct such a unique list in simpler ways. For example,

allroots = []
for period in roots:
allroots += period
pic = list_plot(allroots)


Or just

pic = list_plot(flatten(roots))

more