# Plotting a complex matrix with phase dependent color

I want to plot a complex matrix, i.e., a matrix with complex number entries. I want the matrix to be plotted as DiscretePlot3D as shown in the figure (top figure). But, I also want each bar to be colored based on the phase of the complex number (bottom figure). If you want to try it out, here is the matrix data: {{49.409 + 8.47185 I, 43.8837 + 0.085242 I, 36.0617 + 13.6225 I, 0. + 0. I}, {43.8837 + 0.085242 I, 12.6563 - 21.9037 I, 0. + 0. I, -36.0617 - 13.6225 I}, {36.0617 + 13.6225 I, 0. + 0. I, -12.6563 + 21.9037 I, -43.8837 - 0.085242 I}, {0. + 0. I, -36.0617 - 13.6225 I, -43.8837 - 0.085242 I, -49.409 - 8.47185 I}}

• What is meaning of the height or color? Feb 22, 2023 at 0:08
• I am sorry. The height of the bars in the blue 3D plot are the absolute values of complex number. The color in the bottom figure is the argument (phase) of the complex number. I would like to make a 3D plot like shown in top figure and color each bar with the color shown in the bottom plot. Feb 22, 2023 at 0:29
• Can you create the first plot? Can you create the second plot? It's not very clear what it is that you cannot solve.
– bmf
Feb 22, 2023 at 1:49
• I can create both the plots. However, what I want is to create a 3D plot like the first one but color it using colors taken from the second plot. So Basically two different sets of data to specify height and color. Feb 22, 2023 at 2:21
• Welcome to the Mathematica Stack Exchange. The introductory book written by the inventor is a good learning resource. There is a fast intro for math students as well as a fast intro for programmers to choose from.
– Syed
Feb 22, 2023 at 3:44

clist = {{49.409 + 8.47185 I, 43.8837 + 0.085242 I,
36.0617 + 13.6225 I, 0. + 0. I}, {43.8837 + 0.085242 I,
12.6563 - 21.9037 I,
0. + 0. I, -36.0617 - 13.6225 I}, {36.0617 + 13.6225 I,
0. + 0. I, -12.6563 + 21.9037 I, -43.8837 - 0.085242 I}, {0. +
0. I, -36.0617 - 13.6225 I, -43.8837 - 0.085242 I, -49.409 -
8.47185 I}};

cdata = Flatten[#, 1] &@
MapIndexed[{Sequence @@ #2, Sequence @@ AbsArg[#1]} &, clist, {2}];

Graphics3D[{Opacity[0.3]
, {Hue[#[[4]]]
, Cuboid[{#[[1]] - 0.5, #[[2]] - 0.5, 0}
, {#[[1]] + 0.5, #[[2]] + 0.5, #[[3]]}
]} & /@ cdata
}
, Axes -> True
, Boxed -> True
, BoxRatios -> {1, 1, 1}
]


• For rescaling the Hue, you can try: Hue[Rescale[Mod[#[[4]], 2 \[Pi]], {0, 2 \[Pi]}]] and please let me know if it suits better. If so, I will update the answer.
– Syed
Feb 22, 2023 at 5:41

Not perfect, but perhaps will get you started.

Code:

datatable = complexlist;
options = Sequence[
ColorFunction -> "TemperatureMap",
Frame -> False,
ImageSize -> 300,
Mesh -> None];
texture1 = ListDensityPlot[
Arg /@ complexlist,
Mesh -> Full,
ColorFunction -> "Rainbow",
ImageSize -> 300,
Frame -> False];
lstplt1 = ListPlot3D[
Abs[complexlist],
InterpolationOrder -> 0,
Mesh -> Full,
BoxRatios -> 1,
PlotStyle -> Texture[texture1],
Filling -> Axis,
FillingStyle -> "Rainbow",
ImageSize -> 300];
Grid@{{texture1, lstplt1}}


Data

complexlist = {{49.409 + 8.47185 I, 43.8837 + 0.085242 I,
36.0617 + 13.6225 I, 0. + 0. I}, {43.8837 + 0.085242 I,
12.6563 - 21.9037 I,
0. + 0. I, -36.0617 - 13.6225 I}, {36.0617 + 13.6225 I,
0. + 0. I, -12.6563 + 21.9037 I, -43.8837 - 0.085242 I}, {0. +
0. I, -36.0617 - 13.6225 I, -43.8837 - 0.085242 I, -49.409 -
8.47185 I}};

• Thank you so much for the code. The vertical bars I got are all opaque and purple with only their top surface. However, what I am trying to draw is a simpler plot. I need slightly transparent bars with same color as obtained using texture. I am trying to modify the code but you are really good at this. I will try to edit it to get what I need. Thank you so much. Feb 22, 2023 at 3:11

I define color to approximate the colors in the OP. The range of Arg is -Pi to Pi, so I defined color to use that instead of 0 to 2*Pi.

color=Function[{arg},
Blend[{RGBColor[0.55,0.7,0.55],RGBColor[0.75,0.8,0.55],RGBColor[0.85,0.75,0.4],RGBColor[0.9,0.6,0.096],RGBColor[0.75,0.2,0.07]},Rescale[arg,{-Pi,Pi}]]
];
BarLegend[{color,{-Pi,Pi}}]


Then based on the solution from syed we have my solution below. I avoid using slots such as #4 since novices are not familiar with them.

clist={{49.409+8.47185 I,43.8837+0.085242 I,36.0617+13.6225 I,0.+0. I},{43.8837+0.085242 I,12.6563-21.9037 I,0.+0. I,-36.0617-13.6225 I},{36.0617+13.6225 I,0.+0. I,-12.6563+21.9037 I,-43.8837-0.085242 I},{0.+0. I,-36.0617-13.6225 I,-43.8837-0.085242 I,-49.409-8.47185 I}};
makeData=Function[{z,position},{Sequence@@position,Sequence@@AbsArg[z]}];
cdata = Flatten[MapIndexed[makeData, clist, {2}], 1];
makeCuboid=Function[{re,im,abs,arg},{color[arg],Cuboid[{re-0.5,im-0.5,0},{re+0.5,im+0.5,abs}]}];
Graphics3D[{Opacity[0.7],makeCuboid@@@cdata},Axes->True,Boxed->True,BoxRatios->{1,1,1}]


However, instead of the color scheme above I recommend this.

ArgHue=ResourceFunction["ArgHue"];
BarLegend[{ArgHue,{-Pi,Pi}}]


ArgHue above makes it easier for your eye to approximate phase from the color.