Plotting multidimensional tables

I have a tridiagonal matrix created by

Amat[x_, y_, a_, n_] := SparseArray[{Band[{1, 2}] -> x}, {n, n}]
+SparseArray[{Band[{2, 1}] -> y}, {n, n}]
+SparseArray[{Band[{1, 1}] -> a}, {n, n}];

Here x,y,a are real numbers and n is an integer. To plot eigenvalues of this matrix at n=2, I use

r1 = Table[{x, Sort[Re[Eigenvalues[Amat[x, 1.2, 0.3, 2]]]][[1]]}, {x, -4,
2, 6/10}];
r2 = Table[{x, Sort[Re[Eigenvalues[Amat[x, 1.2, 0.3, 2]]]][[2]]}, {x, -4,
2, 6/10}];

g1 = ListLinePlot[r1,PlotStyle -> Directive[Blue, Thick, Opacity[0.95]], Mesh -> False];
g2 = ListLinePlot[r2,PlotStyle -> Directive[Blue, Thick, Opacity[0.95]], Mesh -> False];

Show[g1, g2, AspectRatio -> 1, Frame -> True, PlotRange -> All]

The problem with this way of plotting is that I have to modify my script for each n value to collect other eigenvalues, similar to (r1,r2) and (g1,g2). Do you have a suggestion on improving this script so that I can plot all eigenvalues with a given symbolic variable n? For example, when n=5, the matrix has five eigenvalues. I would like to have a script that generates a single plot with all five eigenvalues in this case.

Also, is there a way to plot all eigenvalues inrn, given by

rn = Table[{x, Sort[Re[Eigenvalues[Amat[x, 1.2, 0.3, n]]]]}, {x, -4, 2, 6/10}];

as a function of x?

Solution after discussion with the author of the OP

Amat[x_, y_, a_, n_] :=
SparseArray[{Band[{1, 2}] -> x}, {n, n}] +
SparseArray[{Band[{2, 1}] -> y}, {n, n}] +
SparseArray[{Band[{1, 1}] -> a}, {n, n}];
r[n_, part_] :=
Table[{x,
Sort[Re[Eigenvalues[Amat[x, 1.2, 0.3, n]]]][[part]]}, {x, -4, 2,
6/10}]
g[n_, part_] :=
ListLinePlot[r[n, part],
PlotStyle -> Directive[Blue, Thick, Opacity[0.95]], Mesh -> False]
show[n_] :=
Show[Table[g[n, ii], {ii, 1, n}], AspectRatio -> 1, Frame -> True,
PlotRange -> All]

Reproducing the result of the OP:

show[2]

Higher-order values for $$n$$

Grid[{Table[show[xx], {xx, 2, 7}]}]

and even higher values

Partition[Table[show[xx], {xx, 2, 21}], 3]

Checking the validity of the suggested solution.

Here we follow the steps of the author of the OP and create the cases n=3 and n=4 manually to check the answer. The n=2 case is explicitly presented in the OP.

• n=3

We have

r1 = Table[{x,
Sort[Re[Eigenvalues[Amat[x, 1.2, 0.3, 3]]]][[1]]}, {x, -4, 2,
6/10}];
r2 = Table[{x,
Sort[Re[Eigenvalues[Amat[x, 1.2, 0.3, 3]]]][[2]]}, {x, -4, 2,
6/10}];
r3 = Table[{x,
Sort[Re[Eigenvalues[Amat[x, 1.2, 0.3, 3]]]][[3]]}, {x, -4, 2,
6/10}];
g1 = ListLinePlot[r1,
PlotStyle -> Directive[Blue, Thick, Opacity[0.95]],
Mesh -> False];
g2 = ListLinePlot[r2,
PlotStyle -> Directive[Blue, Thick, Opacity[0.95]],
Mesh -> False];
g3 = ListLinePlot[r3,
PlotStyle -> Directive[Blue, Thick, Opacity[0.95]],
Mesh -> False];
Show[g1, g2, g3, AspectRatio -> 1, Frame -> True, PlotRange -> All]

• n=4

We have

r1 = Table[{x,
Sort[Re[Eigenvalues[Amat[x, 1.2, 0.3, 4]]]][[1]]}, {x, -4, 2,
6/10}];
r2 = Table[{x,
Sort[Re[Eigenvalues[Amat[x, 1.2, 0.3, 4]]]][[2]]}, {x, -4, 2,
6/10}];
r3 = Table[{x,
Sort[Re[Eigenvalues[Amat[x, 1.2, 0.3, 4]]]][[3]]}, {x, -4, 2,
6/10}];
r4 = Table[{x,
Sort[Re[Eigenvalues[Amat[x, 1.2, 0.3, 4]]]][[4]]}, {x, -4, 2,
6/10}];
g1 = ListLinePlot[r1,
PlotStyle -> Directive[Blue, Thick, Opacity[0.95]],
Mesh -> False];
g2 = ListLinePlot[r2,
PlotStyle -> Directive[Blue, Thick, Opacity[0.95]],
Mesh -> False];
g3 = ListLinePlot[r3,
PlotStyle -> Directive[Blue, Thick, Opacity[0.95]],
Mesh -> False];
g4 = ListLinePlot[r4,
PlotStyle -> Directive[Blue, Thick, Opacity[0.95]],
Mesh -> False];
Show[g1, g2, g3, g4, AspectRatio -> 1, Frame -> True,
PlotRange -> All]

• Thanks! Could you provide an explanation for your script? Could you also plot all Eigenvalues for a fixed n, say n=5, in one plot instead of multiple panels? Apr 14, 2022 at 18:56
• @Shasa I am not sure what is not clear, so it would be very helpful if you could explain what comments you'd like to see. Essentially, I just read the code you provided, tested it and used functions so I can change automatically the values of n and the Part command. This is why show[2] reproduces both of the plots you needed to manually generate. Likewise for higher n values. The n=5 is the last value of the GraphicsGrid. Have I misunderstood what you asked?
– bmf
Apr 14, 2022 at 19:04
• When n=3, the matrix has three eigenvalues. I would like to have a plot with three eigenvalues in this case. Similarly, for larger values, e.g., n=5, I would like to have a single plot with all five eigenvalues. As inshow two g are manually inserted, then I can't get all eigenvalues in one plot, right? Apr 14, 2022 at 19:13
• @Shasa oh I see. I misunderstood what you wrote in the OP. I am sorry. Let me think a bit about that and I will revisit. In the meantime, I will delete this answer so others can notice. Just let me know when you see this message so I can delete
– bmf
Apr 14, 2022 at 19:37
• The updated answer is very good. Thanks! Apr 15, 2022 at 7:26