I need to visualize a sequence, with the ability to emphasize certain segments.

For example, take this sequence seq:


The list of segments I want to emphasize:


I'm thinking that something that looks like this would work for the emphasis, where the black line is the length of the sequence, and the blue boxes are the segments:

enter image description here

To get start and ending positions of each segment, we can use StringPosition[seq, #] & /@ segments.

Now I'm not sure which visualization tool would help me visualize this sequence and emphasize the segment using these start and end positions.

  • $\begingroup$ how about StringReplace[seq, s : Alternatives @@ segments :> ToString[Style[s, Red, Bold], StandardForm]]? $\endgroup$ – kglr Oct 14 '18 at 1:37
  • $\begingroup$ That's definitely good, and I actually already had something similar to that, but I need something that's more pictorial, thanks though! @kglr $\endgroup$ – brienna Oct 14 '18 at 1:39
  • $\begingroup$ I just noticed that this non-pictorial expression would not work with overlapping segments, but your answer below does. @kglr $\endgroup$ – brienna Oct 14 '18 at 2:18
Graphics[{Gray, EdgeForm[Gray], FaceForm[LightBlue], 
   SequenceReplace[Flatten[Join[{1}, {#[[1]] - 1, 
     Rectangle[{#[[1]], -5}, {#[[2]], 5}], #[[2]] + 1} & /@ 
   StringPosition[seq, Alternatives @@ segments], {StringLength @ seq}]],
 {a_Integer, b_Integer} :> Line[{{a, 0}, {b, 0}}]]}, ImageSize -> 800]

enter image description here


Graphics[{EdgeForm[Gray], FaceForm[LightBlue], 
  Rectangle[{#[[1]], -5}, {#[[2]], 5}]& /@ StringPosition[seq, Alternatives @@ segments]},
  Axes -> {True, False}, Ticks -> None, 
  PlotRange -> {{0, StringLength @ seq}, Automatic}, ImageSize -> 800, 
  Method -> {"AxesInFront" -> False}]

enter image description here

Alternatively, use NumberLinePlot:

NumberLinePlot[Interval/@StringPosition[seq,  Alternatives @@ segments], 
 Spacings -> 0, Ticks -> None, ImageSize -> 600, 
 PlotStyle -> Directive[Opacity[1], PointSize[0], CapForm["Butt"], Thickness[.02]], 
 PlotRange->{{0, StringLength @ seq}, Automatic}]

enter image description here

  • $\begingroup$ Just had to adjust image size. This answer is amazing because it works for the rest of my segments, as well, which overlap... And it works. $\endgroup$ – brienna Oct 14 '18 at 2:04

seq must be a string

Graphics[{Blue, Line[{{0, 15}, {StringLength@seq, 15}}], 
Rectangle[{#, 0}, {#2, 30}] & @@@ StringPosition[seq, segments]}]   

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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