I need to export two versions of a plot as PNG images and have them line up perfectly in an external program. One version needs to have gridlines and a frame at specific points, and the other version needs to have the graph itself extend beyond the gridlines and the frame. Each PNG should line up perfectly when overlayed in an external image editing program (pixel for pixel).

For example, the grid and frame should go from -2pi to 2pi, but the graph should be plotted from -3pi to 3pi (on x and y).

Example code:

g1[x_, t_] := Tan[x]*Sin[2 Pi t] + Cot[x]*Cos[2 Pi t];
rs1 := {x, -2 \[Pi], 2 \[Pi]};
rx1 := {-2 \[Pi], 2 \[Pi]};
ry1 := rx1;
b = rx1[[2]] - rx1[[1]];
b1 = Table[i*b/8 + rx1[[1]], {i, 7}]
a1 = Plot[g1[x, 0.01], Evaluate[rs1], PlotRange -> {rx1, ry1}, 
   PlotStyle -> {Purple}, Axes -> False, AspectRatio -> Automatic, 
   Frame -> True, FrameTicks -> {b1, b1}, FrameStyle -> White, 
   FrameTicksStyle -> Black, GridLines -> {b1, b1}];
a2 = Plot[g1[x, 0.01], {x, -3 \[Pi], 3 \[Pi]}, 
   PlotRange -> {{-3 \[Pi], 3 \[Pi]}, {-3 \[Pi], 3 \[Pi]}}, 
   Axes -> False, AspectRatio -> Automatic];
Overlay[{a1, a2}]

This is close to what I want, but the purple and blue don't line up.

enter image description here

I also tried exporting them as separate PNG images with the resolution multiplied by 1.5 (since 3pi/2pi = 1.5), but that just barely doesn't line up either:

enter image description here

As you can see, it starts to misalign near the edges. I need it to align perfectly.

If I can figure out how to get it to align perfectly with overlay, then I should hypothetically be able to export two versions of the overlay with the same resolution, one where the grid and purple lines are invisible, and the other where the blue lines are invisible, so then I can overlay them in my external program and have the purple and blue line up perfectly.

Does that make sense? How can I have the overlay align perfectly?

If you're wondering the reason I have this requirement, where the plot by itself extends beyond the frame and grid, is because I'm animating this and applying a glow effect to the plot, but not the grid; the glow flickers when pixels reach the edge of PNG image and cross outside of it (since glow is a post effect, it can only operate on pixels that are in the frame), so what I'm doing is applying the glow to a larger version of the plot and then masking the composite down to the size of the frame. The parts of the glow that flicker don't end up being seen since they're masked out.


1 Answer 1


Use the graphics primitives from a2 as Prolog in Show and use the options PlotRangeClipping -> False and ImagePadding -> ... to prevent clipping:

Show[a1, Prolog -> First @ a2, ImagePadding -> 70, PlotRangeClipping -> False]

enter image description here

  • $\begingroup$ Excellent, thank you! $\endgroup$ Apr 14, 2021 at 23:24

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.