7
$\begingroup$

I am trying to represent four functions with different plot styles, But when the functions touch x-axis, it is becoming difficult to distinguish the functions. how to overcome this problem?. I tried shifting the functions in y-axis, but the moment when I use PlotRange ->All the problem reappears.

m1 = Piecewise[{{-1.8520469275761053`*^-27 Cos[
        5.347391750782036` x] + 
      6.642144655629139`*^-12 Sin[5.347391750782036` x], 
     x <= 0.4125`}, {5.347112918701602`*^-12 Cos[
        5.347391750782036` (-0.4125` + x)] + 
      1.` Sin[5.347391750782036` (-0.4125` + x)], x > 0.4125`}}];

m2 = Piecewise[{{2.70223120312319`*^-16 Cos[7.615982190502248` x] + 
      1.` Sin[7.615982190502248` x], 
     x <= 0.4125`}, {7.615982194424732`*^-12 Cos[
        7.615982190502248` (-0.4125` + x)] - 
      1.8475302667032957`*^-12 Sin[7.615982190502248` (-0.4125` + x)],
      x > 0.4125`}}];

m3 = Piecewise[{{6.244447705938866`*^-27 Cos[10.694783501564071` x] - 
      1.1197524383853221`*^-11 Sin[10.694783501564071` x], 
     x <= 0.4125`}, {1.06942258373537`*^-11 Cos[
        10.694783501564071` (-0.4125` + x)] + 
      1.` Sin[10.694783501564071` (-0.4125` + x)], x > 0.4125`}}];


m4 = Piecewise[{{-5.404457624399228`*^-16 Cos[
        15.231964381004495` x] - 1.` Sin[15.231964381004495` x], 
     x <= 
      0.4125`}, {1.523196438056986`*^-11 Cos[
        15.231964381004495` (-0.4125` + x)] + 
      2.954602052227571`*^-11 Sin[15.231964381004495` (-0.4125` + x)],
      x > 0.4125`}}];

s[1] = PlotStyle -> {Black, Thickness[0.004], AbsoluteDashing[{2, 7}]};
s[2] = PlotStyle -> {Black, Thickness[0.004], 
    AbsoluteDashing[{8, 20}]};
s[3] = PlotStyle -> {Black, Thickness[0.004], 
    AbsoluteDashing[{4, 18, 40}]};
s[4] = PlotStyle -> {Black, Thickness[0.004]};

p[1] = Plot[m1, {x, 0, L1}, Evaluate@s[1]];
p[2] = Plot[m2, {x, 0, L1}, Evaluate@s[2]];
p[3] = Plot[m3, {x, 0, L1}, Evaluate@s[3]];
p[4] = Plot[m4, {x, 0, L1}, Evaluate@s[4]];
p[5] = Table[
  Show[p[i] /. {x_Real, y_Real} :> {x/L1, 
      y/Max@Abs@Last@PlotRange@p[i]}, PlotRange -> All], {i, 1, 4}]

Show[p[5], PlotRange -> All]
$\endgroup$
  • $\begingroup$ Cant, we customize the style according to our requirements. because the rest of the plot in my paper is in the plot style that I defined using AbsoulteDashing. I would be easy if I change this one plot according to the style I defined. $\endgroup$ – Vijay Kumar S Apr 4 at 7:55
4
$\begingroup$

See if this can be of any help. A slightly modified version of kglr's first plot.

I have tried to use complementary colors and a mixture of dashed lines with solid ones. You can choose your color combination.

L1 = 1;
Plot[{m1, m2, m3, m4}, {x, 0, L1}, 
 PlotStyle -> {
   Directive[AbsoluteThickness[2], Opacity[1], Red, Dashed], 
   Directive[AbsoluteThickness[5], Opacity[.6], Darker@Blue], 
   Directive[AbsoluteThickness[5], Opacity[.6], Darker@Yellow], 
   Directive[AbsoluteThickness[2], Opacity[1], Green, Dashed]}, 
 ImageSize -> Large, PlotRange -> All, PlotLegends -> "Expressions", 
 Axes -> False, Frame -> True]

enter image description here

| improve this answer | |
$\endgroup$
3
$\begingroup$

Update: "... change this one plot according to the style I defined"

If you replace PlotTheme -> "Monochrome" in methods 1 and 2 in the original answer with

PlotStyle -> (PlotStyle /. ( {s@#} & /@ Range[4]))

method 1 gives:

enter image description here

and method 2 gives:

enter image description here

Original answer:

Assuming a monochrome plot is desired, you can use the options Axes -> False and PlotTheme -> "Monochrome" and

1. shift each function vertically by a small amount:

Plot[{m1 + .01, m2 + .01, m3 - .01, m4 - .01}, {x, 0, 1}, 
 PlotTheme -> "Monochrome", ImageSize -> Large, PlotRange -> All, 
 PlotLegends -> {"m1", "m2", "m3", "m4"}, Axes -> False, Frame -> True] 

enter image description here

2. perturb only the flat portions of the lines slightly:

plot = Chop @ Plot[{m1, m2, m3, m4}, {x, 0, 1}, PlotTheme -> "Monochrome", 
   ImageSize -> Large, PlotRange -> All, 
   PlotLegends -> "Expressions", Axes -> False, Frame -> True];

pairs = GatherBy[Cases[plot, Line[{{_, 0} ..}], All] , #[[1, 1]] &];

perturb[prs_, δ_: .01] := ReplaceAll[Join @@ 
   ({# -> Translate[#, {0, δ}], #2 -> Translate[#2, {0, -δ}]} & @@@ prs)];

perturb[pairs] @ plot 

enter image description here

3. play with thickness and opacity combinations in setting PlotStyle:

Plot[{m1, m2, m3, m4}, {x, 0, 1}, 
 PlotTheme -> "Monochrome", 
 PlotStyle -> {Directive[CapForm["Round"], AbsoluteThickness[2], Opacity[.7]], 
   Directive[CapForm["Round"], AbsoluteThickness[5], Opacity[.3]], 
   Directive[CapForm["Round"], AbsoluteThickness[5], Opacity[.5]], 
   Directive[AbsoluteThickness[1], Opacity[1]]}, ImageSize -> Large, 
 PlotRange -> All, PlotLegends -> "Expressions", Axes -> False, Frame -> True]

enter image description here

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ not OP, but maybe also include colors to enhance distinguishability? $\endgroup$ – exp ikx Apr 4 at 8:11
  • 1
    $\begingroup$ @expikx, good point. I assumed a monochrome plot is desired. If colors are allowed it would be much easier to distinguish the curves. $\endgroup$ – kglr Apr 4 at 9:57
2
$\begingroup$

When one plots several functions that are equal in some domains, the problem is that the markers (and also mesh, dashing ...) are exactly at the same place for each function, so they hide each others.

Here is a solution that usesParametricPlot instead of Plot to create a offset between the mesh-points of each function.

L1 = 1;
offset = 0.1;
gr=ParametricPlot[Evaluate[{{x, m1}
   , {x, m2 } /. {x -> offset + x}
   , {x, m3 } /. {x -> 2 offset + x}
   , {x, m4 } /. {x -> 3 offset + x}
   }]
 , {x, -0.5, L1}
 , AspectRatio -> 1, ImageSize -> 300, 
 PlotRange -> {{0, 1}, Automatic}, Mesh -> 10,
 MeshStyle -> AbsolutePointSize[10], 
 PlotStyle -> {Black, Red, Green, Blue}]  

enter image description here

I prefer to have a distinct marker shape for each function. Here is a try and error postprocessing approach :

Extract the points from the graphic :

 Cases[gr, Point[___], {1, -1}] 

{Point[{2727, 2728, 2729, 2730, 2731, 2732, 2733, 2734, 2735, 2736}],

Point[{2737, 2738, 2739, 2740, 2741, 2742, 2743, 2744, 2745, 2746}],
Point[{2747, 2748, 2749, 2750, 2751, 2752, 2753, 2754, 2755, 2756}],
Point[{2757, 2758, 2759, 2760, 2761, 2762, 2763, 2764, 2765, 2766}]}

There are four series of points corresponding to the four functions. These points can be replaced by something else, say Inset[]. One can copy-paste this serie of points and apply a rule on it :

gr/. Point[data : {2727, 2728, 2729, 2730, 2731, 2732, 2733, 2734, 2735,
      2736}] :> (Inset["X", #] & /@ data)  

enter image description here

| improve this answer | |
$\endgroup$
2
$\begingroup$

If colors are allowed:

You can post-process Plot output to make one the coincident lines dashed so that both lines are visible:

ClearAll[makedashed]
makedashed[prs_, dashing_: {20, 20}] := ReplaceAll[#2 -> 
     {AbsoluteDashing[dashing], #2} & @@@ prs] @* Chop

pairs = GatherBy[Cases[Chop @ plt, Line[{{_, 0} ..}], All], #[[1, 1]] &];

plt = Plot[{m1, m2, m3, m4}, {x, 0, 1}, 
   PlotStyle -> (Directive[Opacity[1], AbsoluteThickness[2], #] & /@
       {Red, Green, Blue, Purple}), 
   ImageSize -> Large, Axes -> False, Frame -> True, PlotLegends -> "Expressions"];

makedashed[pairs] @ plt

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ So far I the customized line style I have defined cannot be implemented in the above suggested answer $\endgroup$ – Vijay Kumar S Apr 5 at 6:14
  • $\begingroup$ @VijayKumarS, please see the update to my previous answer. $\endgroup$ – kglr Apr 5 at 6:36

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.