6
$\begingroup$

Basically i want to change the colour of a single line or 'band' in a ListLinePlot. The first image is the graph i have and the second is the format i'm trying to put the minimum and maximum negative lines in. Thanks in advance

Graph 1

Graph 2

Edit - My code:

zigzaghamiltonian[n_, k_, t2_, phi_, delta_] := Table[
   If[Mod[u, 2] != 0 && v == (u + 1), -2*Cos[k/2],
        If[Mod[u, 2] == 0 && v == (u - 1), -2*Cos[k/2],
        If[Mod[u + v - 1, 4] == 0 && Abs[u - v] < 2, -1,
      If[
       Mod[u, 2] != 0 && (v == (u + 2) || v == (u - 2)), -t2*2*
        Cos[phi + k/2],
       If[
        Mod[u, 2] == 0 && (v == (u + 2) || v == (u - 2)), -t2*2*
         Cos[k/2 - phi], 
        If[Mod[u, 2] != 0 && u == v, -t2*2*Cos[phi + k] + delta, 
         If[Mod[u, 2] == 0 && u == v, -t2*2*Cos[k - phi] - delta, 
          0]]]]]]],
   {u, 2*n}, {v, 2*n}];
vx[n_, k_, t2_, phi_] := Table[
   If[Mod[u, 2] != 0 && v == (u + 1), -Sin[k/2],
        If[Mod[u, 2] == 0 && v == (u - 1), Sin[k/2],
     If[Mod[u, 2] != 0 && v == (u + 2), -t2*Sin[k/2]*Exp[-I*phi],
      If[Mod[u, 2] == 0 && v == (u + 2), -t2*Sin[k/2]*Exp[I*phi], 
       If[Mod[u, 2] != 0 && v == (u - 2), -t2*Sin[k/2]*Exp[I*phi],
        If[Mod[u, 2] == 0 && v == (u - 2), -t2*Sin[k/2]*Exp[-I*phi], 
         If[Mod[u, 2] != 0 && u == v, -t2*2*Sin[phi + k], 
          If[Mod[u, 2] == 0 && u == v, -t2*2*Sin[k - phi], 0]]]]]]]],
   {u, 2*n}, {v, 2*n}];
oka[n_] := 
  Table[If[Mod[u, 2] != 0 && v == u, 1, 0], {v, 2*n}, {u, 2*n}];
okb[n_] := 
  Table[If[Mod[u, 2] == 0 && v == u, 1, 0], {v, 2*n}, {u, 2*n}];

nvalue = 15;
k = Range[0, 2*Pi, 2*Pi/99];
delta = 0.0;
phi = Pi/2;
t2 = 0.00;
eigvals = 
  Table[N[
    Re[Sort[
      Eigenvalues[
       zigzaghamiltonian[nvalue, k[[i]], t2, phi, delta]]]]], {i, 
    100}] ;

ListLinePlot[
 Transpose[
  Table[{k[[i]], eigvals[[i]][[j]]}, {i, 0, 100}, {j, 0, 2*nvalue}]], 
 PlotStyle -> Blue]
$\endgroup$
1
  • 2
    $\begingroup$ Be careful here: {i, 0, 100}, {j, 0, 2*nvalue} Mathematica indexes from 1, not 0. Element 0 is the head e.g {1, 2, 3}[[0]] returns List. You'll get a Part error when you index k or eigvals otherwise. $\endgroup$
    – flinty
    Commented Dec 11, 2021 at 12:48

5 Answers 5

8
$\begingroup$

Your code produces a slightly different plot than the one shown in the question. This seems to do what is desired on the graph generated:

ListLinePlot[
 Transpose[
  Table[{k[[i]], eigvals[[i, j]]}, {i, 1, 100}, {j, 1, 2*nvalue}]], 
 PlotStyle ->    (* N.B. *)
  ReplacePart[   (* if you know the indices... *)
   ConstantArray[Blue, 2*nvalue],
   {nvalue -> Red, nvalue + 1 -> Red}]
 ]

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ If you don't know the indices: ListLinePlot[ Transpose[ Table[{k[[i]], eigvals[[i, j]]}, {i, 1, 100}, {j, 1, 2*nvalue}]], PlotStyle -> ReplacePart[ ConstantArray[Blue, 2*nvalue], Position[#, Alternatives @@ MinMax[#]] &[1/eigvals[[1]]] -> Red]] -- 1/eigvals works in this case because the eigenvalues are separated by zero. $\endgroup$
    – Michael E2
    Commented Dec 11, 2021 at 15:21
2
$\begingroup$

You may give a separate style to every curve.

Here is a simple example:

funs = Table[Sin[x] + i, {i, 4}];
styles = Table[Blue, 4];
styles[[3]] = Red;
Plot[funs, {x, 0, 4 Pi}, PlotStyle -> styles]

enter image description here

$\endgroup$
2
$\begingroup$
nvalue = 15;

table = Transpose[Table[{k[[i]], eigvals[[i]][[j]]}, {i, 1, 100}, {j, 1, 2 nvalue}]];

ListLinePlot[table, 
 PlotStyle -> ArrayPad[{Red, Red}, nvalue - 1, Blue]]

enter image description here

If the indices of the lines you want to color red is not known, as in

shuffledtable = RandomSample[table];

and you want to color the two lines closest to the horizontal axis red, you can:

  1. Sort shuffledtable by the second coordinates of the first points:

ListLinePlot[SortBy[#[[1, -1]] &] @ shuffledtable, 
 PlotStyle -> ArrayPad[{Red, Red}, nvalue - 1, Blue]]

enter image description here

  1. Alternatively, you can use Nearest to identify the two lists nearest the horizontal axis:

shuffledtablestyled = MapAt[Style[#, Red] &, shuffledtable, 
   List /@ Nearest[shuffledtable[[All, 1, 2]] -> "Index", 0, 2]];

ListLinePlot[shuffledtablestyled, PlotStyle -> Blue]

enter image description here

$\endgroup$
2
$\begingroup$

For completeness, here is a manual post-processing approach:

During this animation, I use only Simple Click, Double Click and copy-paste.
The double-click is for selecting the Line interactively.

enter image description here

The two pieces of code that you may not time to read :
theLine = Cases[, Line[___], Infinity]

/. theLine -> {Red, Dashed, AbsoluteThickness[3], theLine}

$\endgroup$
1
$\begingroup$
llp =  ListLinePlot[table, PlotStyle -> Blue, ImageSize -> Large];

Interactively change line colors flipping between Blue and Red using FlipView:

styles = {Directive[Thick, Opacity[1], Blue], Directive[Thick, Opacity[1], Red]};

llp /. l_Line :> 
  MouseAppearance[FlipView @ Thread[{styles, l}], "SampleStyle"]

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.