7
$\begingroup$

Bug fixed in 12.3


I am using v12.1. I printed out the graph using the plot as follows. But there was a strange dirt. This phenomenon occurs in the Sin and Cos functions.

Plot[Sin[3 x], {x, -6, 6}, PlotStyle -> {Red, Thickness[0.05], Opacity[0.5]}]

enter image description here

I tried to run this on Wolfram Player 12.3. The first open state looks the same. However, if you click with a mouse, the dirt on the graph disappears.

The following is the video address for the strange phenomenon I explained.

https://youtu.be/yKldQlLxT_U

What is the cause of the weird output in the plot?

$\endgroup$
3
  • 2
    $\begingroup$ The curves are being chopped up into sub-splines / lines and the end caps don't join up. It seems to be fixed in v13.0 on Windows - somebody should also try it out on 12.3.1. $\endgroup$
    – flinty
    Dec 14, 2021 at 22:09
  • $\begingroup$ if you do not have to use Opacity you can also try PlotStyle -> {Lighter@Lighter@Red, JoinForm["Round"], CapForm["Round"], Thickness[0.05]} $\endgroup$
    – kglr
    Dec 14, 2021 at 22:47
  • $\begingroup$ I do not reproduce the problem with versions 12.3.1 and 13.0.0 on Windows 10 x64. So the bug seems to be fixed in version 12.3. $\endgroup$ Dec 15, 2021 at 7:13

3 Answers 3

8
$\begingroup$

This seems to be a consequence of the adaptive plotting routines. On v. 12.0 (Win10-64) you can prevent that at the cost of the convenient adaptive features of Plot by preventing recursive adjustments to the number of points used in plotting:

Plot[
 Sin[3 x], {x, -6, 6},
 PlotStyle -> Directive[Red, Thickness[0.05], Opacity[0.3]],
 PlotPoints -> 400, MaxRecursion -> 0
]

same plot, no artifacts

$\endgroup$
2
  • 3
    $\begingroup$ The adaptive sampling in Plot is only related, in that it tends to increase the number of points used, and you're more likely to see this as the number of points in the curve increases. $\endgroup$ Dec 14, 2021 at 22:27
  • $\begingroup$ Thank you for your answer. It helped me a lot. $\endgroup$
    – Hoony
    Dec 15, 2021 at 1:48
3
$\begingroup$

There are performance issues drawing lines with large number of points that can vary by operating system. In order to mitigate these, long lines may get split into smaller chunks. If the line is thin or opaque, you generally don't notice. When the line is thick and transparent, you can get artifacts as a result.

Let's parametrize a spiral so that we can control how many points are used to draw it:

In[122]:= spiral[n_] := Table[{t Cos[t], t Sin[t]}, {t, 0, 6 Pi, 6 Pi/(n - 1)}]

In[123]:= Length[spiral[10]]

Out[123]= 10

On my machine, the line splitting happens every 100 points, and you start to see overlaps from the default CapForm:

base = Directive[AbsoluteThickness[20], Red, Opacity[0.33]];

Table[Graphics[{base, Line[spiral[n]]}, 
  PlotLabel -> n], {n, {100, 101, 1000}}]

enter image description here

If you set CapForm[None], the overlap artifacts mostly disappear, but you can now sometimes get "gap" artifacts:

Table[Graphics[{base, CapForm[None], Line[spiral[n]]}, 
  PlotLabel -> n], {n, {100, 101, 1000}}]

enter image description here If you set CapForm["Round"], you can get an interesting effect:

Table[Graphics[{base, CapForm["Round"], Line[spiral[n]]}, 
  PlotLabel -> n], {n, {100, 101, 1000}}]

enter image description here In general these behaviors may vary between versions and across operating systems.

$\endgroup$
2
  • $\begingroup$ So, is this a bug or "expected behaviour"? $\endgroup$ Dec 15, 2021 at 18:02
  • 1
    $\begingroup$ It's expected, but not necessarily desirable. But neither are some of the alternatives... $\endgroup$ Dec 15, 2021 at 19:06
2
$\begingroup$

Version 12.2.0 Win7x64; The following could be version dependent so please consider it an extended comment.


With MaxRecursion->0, and for exploring the comment by Brett Champion;

Plot[Sin[2 x], {x, -4, 4}
 , PlotStyle -> {Red, Thickness[0.02]
   , Opacity[0.5]}
 ,  Mesh -> All
 , PlotPoints -> {1000, 501}
 , ImageSize -> Large
 , MaxRecursion -> 0
 ] 

The first argument to PlotPoints is (?) ignored. You can increase it to 100000 if you want.

For the second argument 501 is the number for Sin Cos plots where the square artifacts appear, but it doesn't hold for Tan plot. The Tan plot seems to be the best behaved one (even with MaxRecursion on). Someone with a developer's knowledge of the algorithm could say more.

Further I tried:

Plot[x^2, {x, -4, 4} ... (x^n same trend)

and the magic number (yet again) is 501 where the square artifacts first appear. For Cosh, Sinh etc the number is slightly higher at around 620.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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