Message Boards Message Boards

[✓] Use Show to change the style of a number line plot?

GROUPS:

Hi everyone,

I have a simple number line plot.

numLinePlot = 
 NumberLinePlot[{Interval[{0, 2}], Interval[{2, 4}], 
   Interval[{0, 4}]}]

I can use Show to change the image size and ticks, but I can't get PlotStyle to change the style of the lines. Why doesn't this work?

Show[numLinePlot, PlotStyle -> {Directive[AbsoluteThickness[3], 
    AbsolutePointSize[4], Blue], 
   Directive[AbsoluteThickness[3], AbsolutePointSize[4], Yellow], 
   Directive[AbsoluteThickness[3], AbsolutePointSize[4], Red]}, 
 Ticks -> {{0, 2, 4}, Automatic}, ImageSize -> 450]

Any suggestion would be much appreciated.

Greg

POSTED BY: Gregory Lypny
Answer
3 months ago

Only functions that are "plots"

Information["*Plot" | "*Plot3D"]

can take the options that has word "plot" in them

Information["Plot*"]

So you should put your PlotStyle in NumberLinePlot, and only then use Show. But why if you can put everything in NumberLinePlot? I guess you got your reasons, but the truth is Show cannot take PlotStyle.

numLinePlot=NumberLinePlot[
    {Interval[{0,2}],Interval[{2,4}],Interval[{0,4}]},
    PlotStyle->{
       Directive[AbsoluteThickness[3],AbsolutePointSize[4],Blue],
       Directive[AbsoluteThickness[3],AbsolutePointSize[4],Yellow],
       Directive[AbsoluteThickness[3],AbsolutePointSize[4],Red]},
    Ticks->{{0,2,4},Automatic},
    ImageSize->450]
POSTED BY: Vitaliy Kaurov
Answer
3 months ago

Here's a cute application of Iterator in the GeneralUtilities` package that achieves the restyling of the plot:

With[{styles = GeneralUtilities`ToIterator[
    {Directive[AbsoluteThickness[3], AbsolutePointSize[4], Blue],
     Directive[AbsoluteThickness[3], AbsolutePointSize[4], Yellow],
     Directive[AbsoluteThickness[3], AbsolutePointSize[4], Red]}]},
 numLinePlot /. {Longest[d__?directive], stuff___} :> {d, Read@styles, stuff}
 ]

enter image description here

Of course it relies on the structure of the graphics produced by NumberLinePlot, which can be inspected.

POSTED BY: Michael Rogers
Answer
3 months ago

Hi Vitaliy,

Thanks for the clarification. The reason that I want to be able to modify plots using Show is that I create functions that return an association, and sometimes one or more of the items in the association is a plot. I thought that with Show I could then modify the plot for specific uses, for example, a research workshop, wen, a class presentation, and notes.

Greg

POSTED BY: Gregory Lypny
Answer
3 months ago

Hi Michael,

Maybe it's something about version 11.1.1, but I cannot find ToIterator or GeneralUtilities when I search the documentation. And what does directive, with lowercase d, stand for in your code?

Greg

POSTED BY: Gregory Lypny
Answer
3 months ago

Hi Gregory,

The GeneralUtilities` package is undocumented, although you often get information with ? cmd. Sometimes the only information you get is code, which seems to be the case with the iterator functions. The package has shipped with Mathematica since V10, maybe earlier. What it contains and how the functions work sometimes change from version to version. I think the Iterator functions were introduced in V11. It's possible that the functions in my answer will disappear or work in a different way in future. On the other hand, iterators are a feature of some programming languages, and it's possible they may become a full incorporated feature of WL at some point.

The d is a local variable representing a pattern __?directive, which matches a sequence of one or more elements for which directive returns True. In effect, for each line in the original plot, d is all of the styling directives of the line. The replacement expression {d, Read@styles, stuff} means that the new style is inserted between the original styling and the points & lines (stuff).

POSTED BY: Michael Rogers
Answer
3 months ago

Group Abstract Group Abstract