7
$\begingroup$

I am trying to plot lines with some translucent behavior, for which I set the opacity to some number in PlotStyle. However, this gives me random squares in the plot that are darker than the rest. How can I force this behavior to go away? I am on Mathematica 14.0. A minimum working example is the following

Plot[x^2, {x, 0, 4},PlotStyle -> Directive[Opacity[.5], Thickness[.03]]]

which produces the attached plot, with the square visible near x==1. enter image description here

$\endgroup$
12
  • 1
    $\begingroup$ fyi, I do not see this on windows 10 using V 14 $\endgroup$
    – Nasser
    Commented May 2 at 4:15
  • 1
    $\begingroup$ I can replicate this on Macbook / v14 and it looks like a bug; I would report it to Wolfram. $\endgroup$
    – Victor K.
    Commented May 2 at 5:59
  • 1
    $\begingroup$ In FullForm of your plot do you see more than one line? $\endgroup$ Commented May 2 at 9:36
  • 2
    $\begingroup$ This is a long-standing issue that I remember reporting before. It was fixed at one point then it was broken again. $\endgroup$
    – Szabolcs
    Commented May 3 at 9:26
  • 1
    $\begingroup$ @rhemans, the "broken to many pieces" is mentioned in the accepted answer by Szabolcs. Also, the other answer there gives the official workaround from WRI (CapForm), so this question seems like a very clear duplicate to me ... $\endgroup$
    – Domen
    Commented May 3 at 10:50

2 Answers 2

6
$\begingroup$

CapForm[] fixes it on my Mac:

Plot[x^2, {x, 0, 4}, 
 PlotStyle -> Directive[CapForm["Butt"], Opacity[.5], Thickness[.03]]]

enter image description here

$\endgroup$
9
  • 1
    $\begingroup$ (+1) Is it correct to say that you are avoiding the overlapping region by removing the end caps of the lines instead of joining the multiple lines? $\endgroup$
    – rhermans
    Commented May 2 at 14:06
  • 2
    $\begingroup$ Does not work in all circumstances, when the function has large slope, for example Plot[ArcTan[20 x], {x, -10, 10}, PlotStyle -> Directive[CapForm["Butt"], Opacity[.5], Thickness[.03]]]. See the large gap at the origin? But joining several lines into one line works. $\endgroup$ Commented May 2 at 14:24
  • 1
    $\begingroup$ @MichaelE2 Yes, it works, also CapForm["Butt"] is then redundant. But what is the logic behind it? ArcTan has no discontinuities why would I use Exclusions? $\endgroup$ Commented May 2 at 14:35
  • 1
    $\begingroup$ @rhermans I assume the multiple lines comes from packaging the graphics for the GPU to deal with, since the Graphics contains only one Line[]. But yes, that was my idea, to remove the overlaps with CapForm[]. I suppose the community will agree that it's a bug. $\endgroup$
    – Michael E2
    Commented May 2 at 14:38
  • 2
    $\begingroup$ @azerbajdzan There's a gap with Plot[ArcTan[20 x], {x, -10, 10}, PlotStyle -> Directive[Opacity[.5], Thickness[.03]]], too. It has nothing to do with the issue at hand. CapForm["Butt"] does fix the issue in the OP with this graph, though (the dark blocks from overlapping segments). $\endgroup$
    – Michael E2
    Commented May 2 at 16:49
6
$\begingroup$

I think this is a bug, but there are many workarounds.

It seems to me that the problem is that Mathematica is internally dividing the trace into segments, and the overlapping regions of the thick semi-transparent lines show darker.

While I can not reproduce the problem with your exact code, in Wolfram Cloud I can see the problem with

Plot[
  x^3.
  , {x,-1.,1.}
  , PlotStyle -> Directive[Opacity[.5], Thickness[.03]]
]

enter image description here

More clearly also if using FullForm as suggested by @azerbajdzan

ListPlot@Cases[
   FullForm[Plot[x^3.,{x,-1.,1.}]]
   , Line[x_]:>x
   , All
]

enter image description here

As I wrote in the comments, I have seen that as an annoying feature when exporting plots as vector graphics formats like SVG or PDF, where for no real reason, plot traces are not part of a single object but an arbitrary set of ungrouped elements.

One possible workaround could be replacing {Directive[...],Line[...],Line[...]} with joined lines {Directive[...],Line[...]}

Plot[
  x^3.
  , {x,-1.,1.}
  , PlotStyle -> Directive[Opacity[.5], Thickness[.03]]
] /. {a_,Line[b_], Line[c_]}:>{a,Line[Join[b,c]]}

enter image description here

I'm not very good with patterns, but of there are many artifacts, something like this might work

Plot[
  x^3.
  , {x,-1.,1.}
  , PlotStyle -> Directive[Opacity[.5], Thickness[.03]]
]/. {a_Directive,b___}:>{a,Line[Flatten[{b}[[All,1]],1]]}

If the separation is caused by Exclusions, an alternative solution could be to use the option Exclusions->None

$\endgroup$
7
  • 1
    $\begingroup$ This bug with your code can be reproduced also in version 13.0.1. $\endgroup$ Commented May 2 at 10:50
  • $\begingroup$ Hi rhermans - thanks for the help! Interesting insights - unfortunately the workaround doesn't seem to work on my machine - when I run your code I have a line with 4 artifacts. What is interesting is on my machine when I run my original example with FullForm as you do I only get one line back, rather than two. When I run your example however I do get two lines. $\endgroup$
    – JamesVR
    Commented May 2 at 12:26
  • $\begingroup$ @JamesVR if you have more artefacts, then the ReplaceAll (/. pattern should be different, mine works only for two, but is easier to understand. Probably somebody else can volunteer a pattern that will work for any number of lines. $\endgroup$
    – rhermans
    Commented May 2 at 14:08
  • $\begingroup$ @JamesVR see the update $\endgroup$
    – rhermans
    Commented May 2 at 14:33
  • 1
    $\begingroup$ @JamesVR It is also possible to have two lines (or more) wrapped in a single Line like for example Graphics[Line[{{{0, 0}, {1, 1}}, {{0, 1}, {1, 0}}}]] which is equivalent of Graphics[{Line[{{0, 0}, {1, 1}}], Line[{{0, 1}, {1, 0}}]}]. $\endgroup$ Commented May 3 at 9:39

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