# Mathematica secretly plotting vector graphics outside the axis range

I have code to plot the zero contour of a function. This function is symmetric about the x=0 axis, but I only want to display it in the first quadrant. Additionally, I currently set up the axes as a separate plot and combine this contour, and everything else that I want to plot in a Show command. To limit the plot to the first quadrant I put the plot of the axes first, as shown in the MWE below (I have boiled it down to plotting a quarter of the unit circle):

ClearAll["Global*"];
axesrng = {0, 1.25};  (* I want to create the axes seperately *)
xrngplot = {0.0, 1.0};  (* plot range for the circle plot *)

xyunitcirc[x_, y_] := x^2 + y^2 - 1;
yunitcirc[x_] := y /. Solve[xyunitcirc[x, y] == 0, y];
axes = Plot[{}, {x, axesrng[], axesrng[]},
PlotRange -> {axesrng, axesrng}, AspectRatio -> 1,
ImageSize -> 100, PlotRangePadding -> 0];
testplot =
Show[axes, Plot[yunitcirc[x], {x, xrngplot[], xrngplot[]}]]
Export[ NotebookDirectory[] <> "testplot.pdf", testplot]


The problem is that Mathematica "secretly" plots the part of the contour that is in the fourth quadrant when I save it in a .pdf file (the same thing happens when I save it as a .svg file). This can be seen when I open it in inkscape - I can access the "hidden" part of the contour using the edit node tool: Sometimes this extra part is invisible, but sometimes it's not, as shown in the screenshot below which has the real-life plots I made (i.e. more than the MWE I have provided), and was compiled into a LaTeX document using some process I don't know about that reveals the "secret" parts of the plot; you see that the green curve extends down into the lower plot and the orange curve extends too far above and below the axes. Is there an easy fix to suppress the creation of a graphics vector outside the axes range given by axesrng?

(Obviously one thing I could do is to convert the contour into a polar plot, and plot it between 0 and pi/4, but I would prefer to keep the code in Cartesians and just suppress the parts I don't need.)

Thanks!

• Does using the RegionFunction option help? Oct 22, 2021 at 21:54
• ContourPlot[x^2 + y^2 - 1 == 0, {x, 0, 1}, {y, 0, 1}] Oct 23, 2021 at 2:03
• If I understand what you are saying, after exporting a PDF from Mathematica, you are post-processing it with some software that does not respect the clipping region. This is a bug in that software. Ideally, it would be fixed in that software instead of working around it in Mathematica. Have you reported the bug in the appropriate forum? Oct 23, 2021 at 15:04
• @jms547 That's certainly very annoying. Many publishers seem to use extremely outdated software, and I had problems too. I am not aware of anything else than RegionFunction (as suggested above) that might help within Mathematica. However, I would still suggest not trying to solve this in Mathematica. Clipping masks are very common in vector graphics, and you'll find them in many more places than just constraining the graphics to the plot region. E.g., you'll find a clipping mask on every Text, every label, etc. If that system messes up clipping masks, it is likely to mess up more ... Oct 24, 2021 at 9:05
• ... than just showing out-of-plot-range curves. However, it seems very unlikely that it would mishandle all clipping masks in all scenarios though (otherwise there would be a lot of complaints). Try to find out what triggers the problem. Try converting your PDF to an older version. If they ask for EPS, create the EPS yourself instead of letting them convert the PDF. Try to process the PDF through some program such as Ghostscript, re-export with Acrobat, re-save with Preview.app on macOS, etc. Hopefully one of these will help. The last resort would be rasterizing at high resolution. Oct 24, 2021 at 9:07

you have two plots here combined using Show, the first plot allows the whole range

Plot[yunitcirc[x], {x, xrngplot[], xrngplot[] when using show the other part of the plot will be there but white (don't know why) and using pdf editor you can check that. To avoid this you need to specify the desired PlotRange in each plot passed to Show. So, this should solve the problem, and I checked that with pdf editor it is working fine

Show[axes,
Plot[yunitcirc[x], {x, xrngplot[], xrngplot[]},
PlotRange -> {axesrng, axesrng}]]
` 