ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 23 Mar 2021 21:34:22 +0100Strange list_plot artifact?https://ask.sagemath.org/question/56330/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.Tue, 23 Mar 2021 05:42:09 +0100https://ask.sagemath.org/question/56330/strange-list_plot-artifact/Answer by Juanjo for <p>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.</p>
<p>I have my list <code>roots</code>, 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.)</p>
<p>When I used the code:</p>
<pre><code>allroots=[]
for period in roots:
for i in period:
allroots.append(i)
pic=list_plot([m for m in allroots])
</code></pre>
<p>my graphics come out exactly as desired. <em>However,</em> when I use the following code:</p>
<pre><code>plot = sum(list_plot(i) for i in roots)
</code></pre>
<p>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. </p>
<p>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.</p>
https://ask.sagemath.org/question/56330/strange-list_plot-artifact/?answer=56342#post-id-56342Compare 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))Tue, 23 Mar 2021 21:34:22 +0100https://ask.sagemath.org/question/56330/strange-list_plot-artifact/?answer=56342#post-id-56342Answer by FrédéricC for <p>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.</p>
<p>I have my list <code>roots</code>, 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.)</p>
<p>When I used the code:</p>
<pre><code>allroots=[]
for period in roots:
for i in period:
allroots.append(i)
pic=list_plot([m for m in allroots])
</code></pre>
<p>my graphics come out exactly as desired. <em>However,</em> when I use the following code:</p>
<pre><code>plot = sum(list_plot(i) for i in roots)
</code></pre>
<p>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. </p>
<p>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.</p>
https://ask.sagemath.org/question/56330/strange-list_plot-artifact/?answer=56333#post-id-56333use point2d to plot a single point, not list_plotTue, 23 Mar 2021 08:22:18 +0100https://ask.sagemath.org/question/56330/strange-list_plot-artifact/?answer=56333#post-id-56333Comment by elcardin for <p>use point2d to plot a single point, not list_plot</p>
https://ask.sagemath.org/question/56330/strange-list_plot-artifact/?comment=56339#post-id-56339Thank you! I'm not sure why this fixed it, but it did!Tue, 23 Mar 2021 20:28:22 +0100https://ask.sagemath.org/question/56330/strange-list_plot-artifact/?comment=56339#post-id-56339