Message Boards Message Boards

1
|
3755 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Workaround for render differences between PlotStyle and ColorFunction?

ColorFunction seems to various stylistic changes in Plot (and probably other plot-like graphical constructs) relative to PlotStyle. Here's a fairly minimal example:

plota = Plot[1, {x, 0, 1}, PlotStyle -> Black];
plotb = Plot[1, {x, 0, 1}, ColorFunction -> (Black &)];

Here's plota:

Plot a

And here's plotb:

Plot b

It may not be obvious how different these are, but running them through ImageDifference yields this:

Image difference

And here's an animation which switches between the two every second:

Animated difference

As you can see, in b the vertical axis shifts left, both axes pull away from the image boundary, and get thinner, the ticks on the axes get shorter, the axis labels get smaller, and the graph of the function gets sharper, among others.

Is there a way to force these changes not to happen? Sometimes I really need to use ColorFunction, and I'd really very strongly prefer those uses to be consistent with others (for example, this is absolutely essential when I'm stitching together two animations; my current workaround is to use ColorFunction in place of PlotStyle even when I don't need it, but this is confusing and ugly).

Of course, even better would be for this (presumably unintended) behavior to be fixed, but I realize that's probably hard.

(Though not exactly the same, similar phenomena happen both on my Mac Pro (late 2013) with Mathematica 12.2 and on my MacBook Pro (13-inch, 2016) with Mathematica 12.0. The images above are from the Mac Pro, since it's on the latest version of Mathematica.)

3 Replies

Hello Clayton,

interesting propblem! With

plotb // InputForm

one sees that plotb contains a GraphicsComplex, and this seems to make the difference. If GraphicsComplex is resolved using

Normal[plotb]

then the axes look identical, but strangely the option ColorFunction -> (Black &) appears to be ignored. So if I define

plotc = Normal[plotb] /. RGBColor[__] -> Black

I cannot detect any difference between plota and plotc (at least on my system: "12.1.1 for Linux x86 (64-bit) (June 19, 2020)").

Maybe this is in a way helpful. Merry Christmas! -- Henrik

EDIT: Well, the above might not be that helpful - I basically seem to have converted plotb back to plota. But the strange thing still is the effect of GraphicsComplex.

POSTED BY: Henrik Schachner

Okay, but this doesn't really work if you actually use ColorFunction in a nontrivial way. E.g., consider:

plotd = Plot[1, {x, 0, 1}, ColorFunction -> (GrayLevel[#1] &), ImageSize -> 600]

The axes, ticks, and labels are still transformed like in plotb, and, while the Normal trick fixes that, it destroys the gradient which was the whole point of using ColorFunction to begin with.

In any case, it is helpful (at least, in an abstract way) to know that this is a GraphicsComplex issue. Indeed, upon further investigation it seems pretty clear that the issue is that GraphicsComplex has a weird effect on PlotRangePadding. For example,

Graphics[{Black, AbsoluteThickness[1.6], GraphicsComplex[{{0, 1}, {1, 1}}, Line[{{1, 2}}]]}, 
 ImageSize -> 600, Axes -> True, AspectRatio -> GoldenRatio^(-1)]

doesn't even have the right dimensions, but using the PlotRangePadding options that InputForm reveals that plota is using...

Graphics[{Black, AbsoluteThickness[1.6], GraphicsComplex[{{0, 1}, {1, 1}}, Line[{{1, 2}}]]}, 
 ImageSize -> 600, Axes -> True, AspectRatio -> GoldenRatio^(-1), 
 PlotRangePadding -> {{Scaled[0.02`], Scaled[0.02`]}, {Scaled[0.05`], Scaled[0.05`]}}]

yields an image which is identical to plota.

But, even though this seems like the underlying explanation (subject, of course, to someone who actually knows what's going on saying otherwise), I still can't figure out how to use PlotRangePadding to get plota and plotb to be identical.

When I make a figure in mathematica I always tell myself (basically) that any option that is automatic, might change in the future or be influenced somehow. You can do AbsoluteOptions on both object and inspect what changed and copy those options. You can even set them as default for Plot…

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract