Suppose that I make a plot of $x^2$ vs. $x$:

myPlot = Plot[x^2, {x, -1, 1}, Frame -> True]


If I click on the plot, I see the orange handles with which I can resize the plot:


Now, suppose that I want to actually crop the plot -- I would like to effectively shrink the orange box so that some of the plot is actually cropped. (Why in the world would I want to do this? It's a long story...)

I have found, from answers to a question that I previously asked, that this is possible using ImagePad with negative arguments. For example, suppose that I want to crop 40 pixels off the right side of the plot. I can do the following:

ImagePad[myPlot, {{0, -45}, {0, 0}}]


It looks good so far. However, when I resize the resulting cropped plot by dragging the orange handles, it looks like the image has been rasterized:


But, in contrast, I need the image to remain a vector image (non-rasterized) after cropping. Is that possible? I am running both version 8 and 9.

  • $\begingroup$ Just using PlotRange is no option? Like: Plot[x^2, {x, -1, 1}, Frame -> True, PlotRange -> {{-1, 0.5}, Automatic}] $\endgroup$ – Pinguin Dirk Mar 8 '14 at 16:50
  • $\begingroup$ Or after the fact: Show[plot, PlotRange -> ...] $\endgroup$ – Szabolcs Mar 8 '14 at 16:50
  • $\begingroup$ @PinguinDirk Unfortunately, no, just PlotRange is not an option for me. I need to be able to crop the entire image -- including white space, frames, frame ticks, and frame labels. $\endgroup$ – Andrew Mar 8 '14 at 16:54
  • $\begingroup$ @Andrew I'm not sure I understand. Why can't you just not use frames and labels then? Show[... Frame -> False] $\endgroup$ – Szabolcs Mar 8 '14 at 16:58
  • 2
    $\begingroup$ @Andrew LevelScheme doesn't use Mathematica's special built-in frame. It draws its own frame, so setting PlotRange on the output should in fact crop the frame as well. $\endgroup$ – Szabolcs Mar 8 '14 at 17:06

You can use Inset (this is what the interactive editor does basically).

Example: The variable crop consists of image scaled coordinates in the order

{{xmin, xmax}, {ymin, ymax}}

In this example, the value ymax is greater than one, which extends the image beyond the boundary of the original plot.

myPlot = Plot[x^2, {x, -1, 1}, Frame -> True];
croppedPlot =
 With[{iSize = 350. {1, 1/GoldenRatio},
       crop = {{0.1, 0.8}, {0.2, 1.2}}}, 
    Inset[Show[myPlot, ImageSize -> iSize], {0., 0.}, ImageScaled[{0., 0.}]],
    PlotRange -> crop iSize, ImageSize -> crop iSize]
Framed[croppedPlot, FrameMargins -> 0, FrameStyle -> Red]

Mathematica graphics

| improve this answer | |

I just realized from this Wolfram page that one can crop the "orange box" by Ctrl+dragging one of the orange handles.

myPlot = Plot[x^2, {x, -1, 1}, Frame -> True]


and obtain


This seems to work pretty well for the question that I asked. A programmatic method would be better, but this use of the front end is OK.

| improve this answer | |
  • $\begingroup$ (+1)Very Nice Solution. :) $\endgroup$ – Hosein Rahnama May 20 '17 at 21:41
  • $\begingroup$ How can I export the cropped picture? $\endgroup$ – Hosein Rahnama May 20 '17 at 21:43

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