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):

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[[1]], axesrng[[2]]}, 
   PlotRange -> {axesrng, axesrng}, AspectRatio -> 1, 
   ImageSize -> 100, PlotRangePadding -> 0];
testplot =
 Show[axes, Plot[yunitcirc[x], {x, xrngplot[[1]], xrngplot[[2]]}]]
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:

vector graphics contains the branch outside the desired plot range

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.

the secret graphics vectors can sometimes be revealed

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.)


  • 2
    $\begingroup$ Does using the RegionFunction option help? $\endgroup$ Oct 22 '21 at 21:54
  • 2
    $\begingroup$ ContourPlot[x^2 + y^2 - 1 == 0, {x, 0, 1}, {y, 0, 1}] $\endgroup$
    – cvgmt
    Oct 23 '21 at 2:03
  • 1
    $\begingroup$ 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? $\endgroup$
    – Szabolcs
    Oct 23 '21 at 15:04
  • 1
    $\begingroup$ @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 ... $\endgroup$
    – Szabolcs
    Oct 24 '21 at 9:05
  • 2
    $\begingroup$ ... 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. $\endgroup$
    – Szabolcs
    Oct 24 '21 at 9:07

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

Plot[yunitcirc[x], {x, xrngplot[[1]], xrngplot[[2]]  

enter image description here

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

 Plot[yunitcirc[x], {x, xrngplot[[1]], xrngplot[[2]]}, 
  PlotRange -> {axesrng, axesrng}]]     

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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