3
$\begingroup$

Even when plotting a seemingly simple function, Mathematica seems to screw up the dashing. If we take for instance

Plot[Sqrt[x^2/(1 + x^2)], {x, 0, 10}, PlotRange -> Full, PlotStyle -> Dashed],

the resulting plot has uneven dashing around 2:

Plot with uneven spacing

When dragging a corner of the plot, it becomes apparent that the plotted line consists of two segments. As the segments are dashed individually, it does not line up at the point where the segments meet.

Gif of plot behaviour when dragging a corner

A workaround that I found is to manually evaluate the function on a mesh and doing a ListLinePlot of the results:

ListLinePlot[Table[{x, Sqrt[x^2/(1 + x^2)]}, {x, 0, 10, 0.1}], PlotRange -> Full, PlotStyle -> Dashed]

Plot with workaround

This seems a bit awkward, however, as it is the way Dashed should have behaved in the first place. So my question is: Is there any elegant way to stop Mathematica from separating the plot into segments and consequently screwing with the dashing?

$\endgroup$
5
  • $\begingroup$ I don't see this on v12.2.0-Win7-x86. What is your $Version? $\endgroup$
    – Syed
    Commented Oct 19, 2022 at 13:54
  • $\begingroup$ My version is: 12.3.1 for Mac OS X ARM (64-bit) $\endgroup$
    – Meufel
    Commented Oct 19, 2022 at 13:56
  • 1
    $\begingroup$ This is reproducible with v13.1 on a Mac OS X x86. Changing the value of PlotPoints affects the number of "seams" and their locations. However you would need to experiment with values for PlotPoints for each ImageSize. $\endgroup$
    – Bob Hanlon
    Commented Oct 19, 2022 at 14:04
  • $\begingroup$ It seem my "workaround" does exactly that, changing PlotPoints to a value suitable for the plot size I chose. Using {x, 0, 10, 0.01}, I can reproduce the problem again. $\endgroup$
    – Meufel
    Commented Oct 19, 2022 at 14:33
  • $\begingroup$ Can not see this on Windows 10 MMA version 13.1 $\endgroup$ Commented Oct 19, 2022 at 15:37

2 Answers 2

2
$\begingroup$

Partial answer

This doesn't actually stop the splitting of the plot-line into two parts, but it is an easier way to find a dashing that works and doesn't have the weird redrawing effect on resize.

For PlotStyle, use Dashing instead of Dashed:

Plot[Sqrt[x^2/(1 + x^2)], {x, 0, 10}, PlotRange -> Full, PlotStyle -> Dashing[.03]]

enter image description here

However, compare to this:

Plot[Sqrt[x^2/(1 + x^2)], {x, 0, 10}, PlotRange -> Full, PlotStyle -> Dashing[.02]]

enter image description here

$\endgroup$
1
$\begingroup$

Here's a way to use MeshShading. Unfortunately, you have to transform the geometry so that distance is isotropic:

{aratio, pr} = {AspectRatio, PlotRange} /.
   AbsoluteOptions[
    Plot[Sqrt[x^2/(1 + x^2)], {x, 0, 10}, 
     PlotRange -> Full], {AspectRatio, PlotRange}];
pratio = 1/Ratios[Differences /@ pr][[1, 1]];
Plot[Sqrt[x^2/(1 + x^2)], {x, 0, 10}, PlotRange -> Full, 
 MeshShading -> {Automatic, None}, MeshFunctions -> {"ArcLength"}, 
 Mesh -> 100, MeshStyle -> Opacity[0], 
 ScalingFunctions -> {pratio*aratio # &, 
   #/(pratio*aratio) &}, 
 Ticks -> {Charting`ScaledTicks["Linear", "Standard"], 
   Charting`ScaledTicks["Linear", "Nice"] @@ Last@pr}]
$\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.