Ask Your Question

Strange list_plot artifact?

asked 2021-03-23 05:42:09 +0200

elcardin gravatar image

updated 2021-03-26 21:51:31 +0200

FrédéricC gravatar image

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:

for period in roots:
    for i in period:
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 flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2021-03-23 08:22:18 +0200

FrédéricC gravatar image

use point2d to plot a single point, not list_plot

edit flag offensive delete link more


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

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

answered 2021-03-23 21:34:22 +0200

Juanjo gravatar image

updated 2021-03-23 21:36:02 +0200

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))
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2021-03-23 05:42:09 +0200

Seen: 33 times

Last updated: Mar 23