3
$\begingroup$

The user #Nasser showed an elegant way to plot complex numbers:

data = Table[RandomReal[{-1, 1}] + I RandomReal[{-1, 1}], {30}];

p = ListPlot[{Re[#], Im[#]} & /@ data,
    AxesOrigin -> {0, 0},
    PlotRange -> {{-1.2, 1.2}, {-1.2, 1.2}},
    ImagePadding -> 40,
    AspectRatio -> 1,
    Frame -> True,
    FrameLabel -> {{Im, None}, {Re, "complex plane"}},
    PlotStyle -> Directive[Red, PointSize[.02]]];

Show[p, Graphics@Circle[{0, 0}, 1]]

How can I change this code so that an input of multiple lists is possible and they are plotted each in a different color?

$\endgroup$
0

2 Answers 2

4
$\begingroup$

I propose using this function to process your complex numbers:

complexSplit = Function[z, {Re@z, Im@z}, Listable];

It works on multiple list shapes:

complexSplit[3.7 + 2.1 I]
complexSplit[{3.7 + 2.1 I, 5.7 + 4.4 I}]
complexSplit[{{3.7 + 2.1 I, 5.7 + 4.4 I}, {8.1 + 3.7 I}, 4.2 + 5.1 I}]
{3.7, 2.1}

{{3.7, 2.1}, {5.7, 4.4}}

{{{3.7, 2.1}, {5.7, 4.4}}, {{8.1, 3.7}}, {4.2, 5.1}}

Next I use this to create three lists of complex numbers:

data = {1, I}.# & /@ RandomReal[{-1, 1}, {3, 2, 50}]

Or as Rojo reminded me, more simply:

data = RandomComplex[{-1 - I, 1 + I}, {3, 50}];

I can then plot all three lists in a single ListPlot expression:

ListPlot[complexSplit @ data,
 AxesOrigin -> {0, 0}, 
 PlotRange -> {{-1.2, 1.2}, {-1.2, 1.2}}, ImagePadding -> 40, AspectRatio -> 1, 
 Frame -> True, FrameLabel -> {{Im, None}, {Re, "complex plane"}}, 
 PlotStyle -> (Directive[#, PointSize[.02]] & /@ {Purple, Darker@Green, Orange}),
 Epilog -> Circle[{0, 0}, 1]
]

enter image description here

I can plot only the first list by using:

ListPlot[complexSplit @ {First @ data},
 AxesOrigin -> {0, 0}, 
 PlotRange -> {{-1.2, 1.2}, {-1.2, 1.2}}, ImagePadding -> 40, AspectRatio -> 1, 
 Frame -> True, FrameLabel -> {{Im, None}, {Re, "complex plane"}}, 
 PlotStyle -> (Directive[#, PointSize[.02]] & /@ {Purple, Darker@Green, Orange}),
 Epilog -> Circle[{0, 0}, 1]
]

enter image description here

Note that I added a List level ({ }); this is to keep each point from being plotted as a different color. Note also that the points from the first list are not colored the same as they are in the first plot; if you wish to preserve coloring you can replace any list you do not wish to plot with Indeterminate. Here I will plot the first and third lists using the same coloring as plotting all three:

ListPlot[complexSplit @ ReplacePart[data, 2 -> Indeterminate],
 AxesOrigin -> {0, 0}, 
 PlotRange -> {{-1.2, 1.2}, {-1.2, 1.2}}, ImagePadding -> 40, AspectRatio -> 1, 
 Frame -> True, FrameLabel -> {{Im, None}, {Re, "complex plane"}}, 
 PlotStyle -> (Directive[#, PointSize[.02]] & /@ {Purple, Darker@Green, Orange}),
 Epilog -> Circle[{0, 0}, 1]
]

enter image description here

$\endgroup$
5
  • 2
    $\begingroup$ Just because I know you wouldn't like to have missed a built-in, I have to check: you don't know about RandomComplex or you chose against it? $\endgroup$
    – Rojo
    Commented Oct 24, 2013 at 19:20
  • $\begingroup$ @Rojo Honestly I forgot about it, Dot is the first thing that came to mind and I hoped it would interest the OP. Thanks for reminding me. For some reason I never seem to use random complex numbers; I'll take this as a challenge to change that. $\endgroup$
    – Mr.Wizard
    Commented Oct 24, 2013 at 21:51
  • 1
    $\begingroup$ No problem. Even with Dot, you could also do {1, I}.RandomReal[{-1, 1}, {2, 3, 50}] $\endgroup$
    – Rojo
    Commented Oct 24, 2013 at 23:15
  • $\begingroup$ @Rojo It's just not my day. :^) $\endgroup$
    – Mr.Wizard
    Commented Oct 25, 2013 at 1:11
  • $\begingroup$ This is great! Thank you very much Mr.Wizard. $\endgroup$
    – Fred
    Commented Oct 28, 2013 at 9:59
1
$\begingroup$

Since V 12.1 there is ComplexListPlot

Using Mr. Wizard's data

data = {1, I} . # & /@ RandomReal[{-1, 1}, {3, 2, 50}]; 

ComplexListPlot[data,
 AxesOrigin -> {0, 0},
 AxesStyle -> White,
 Background -> Black,
 Epilog -> {White, Circle[{0, 0}, 1]},
 Frame -> True,
 FrameLabel -> {{Im, None}, {Re, "complex plane"}},
 FrameStyle -> White,
 ImagePadding -> 40,
 PlotMarkers -> Automatic,
 PlotRange -> {{-1.2, 1.2}, {-1.2, 1.2}}]

enter image description here

Another example

ComplexListPlot[
 {NestList[(0.9 + 0.1 I) # &, 1 + I, 30], 
  NestList[(0.9 + 0.2 I) # &, 1 + I, 30]},
 Joined -> {True, False},
 PlotTheme -> "Marketing",
 PlotLegends -> {0.1, 0.2}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.