Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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

share|improve this answer
2  
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? –  Rojo Oct 24 '13 at 19:20
    
@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. –  Mr.Wizard Oct 24 '13 at 21:51
1  
No problem. Even with Dot, you could also do {1, I}.RandomReal[{-1, 1}, {2, 3, 50}] –  Rojo Oct 24 '13 at 23:15
    
@Rojo It's just not my day. :^) –  Mr.Wizard Oct 25 '13 at 1:11
    
This is great! Thank you very much Mr.Wizard. –  Fred Oct 28 '13 at 9:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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