5
$\begingroup$

I have the following plot:

Plot[x^2,{x,0,100}]

Is there a function that determines the plot's current height and width? And how can I change these dimensions?

$\endgroup$

5 Answers 5

2
$\begingroup$

This is an interesting question and I do not think there is a general answer to your request. A simple answer might be to set the ImageSize option. This sets the overall size of the plot. However, once you start adding axis labels, you will need some padding around the plot to accommodate the length of the labels. There are several other parameters that also expand or shrink the plot, for example, to allow for the ticks and the numbers on the axes. Furthermore, as versions have been developed default values have been changed. Have a look at Graphics in help for all the basic options.

I am not sure how deep you wish to go. If you wish to know the lengths of the axes as oppose to the overall dimensions of the plot then all the factors have to be taken into account. I have been wondering for some time if anyone has worked out a formula that will give the axes size given all the possible options and defaults that can be set.

$\endgroup$
2
  • $\begingroup$ What I am really trying to accomplish is to replicate my current plot twice, but change the height and width of the replicates such that they are half the size of the original plot (i.e., they have half the width and height of the original). Is there an easy way to do this? $\endgroup$
    – tardigrade
    May 21 at 21:30
  • $\begingroup$ This is a bit different. We prefer that you ask a new question with a more specific title. This enables others to find your new question and look at the answers. $\endgroup$
    – Hugh
    May 22 at 7:39
7
$\begingroup$

You can use the ResourceFunction GraphicsInformation to obtain this information:

"ImageSize" /. ResourceFunction["GraphicsInformation"] @ Plot[x^2,{x,0,100}]

{360., 212.293}

$\endgroup$
5
$\begingroup$

Finding the height and width of a plot?

I think you are looking for something like the Wolfram Function Repository (WFR) function GraphicsBounds.

It seems that that function does not work for the simple case you show:

gr = Plot[x^2, {x, 0, 100}];
ResourceFunction["GraphicsBounds"][gr]

(* {{2.04082*10^-6, 100.}, {4.16493*10^-12, 10000.}} *)
```
$\endgroup$
1
  • $\begingroup$ The same as RegionBounds@*DiscretizeGraphics $\endgroup$
    – cvgmt
    May 22 at 0:24
4
$\begingroup$

I can suggest another simple solution:

plot = Plot[x^2, {x, 0, 100}]
Association[Options[plot]][PlotRange]

(* {{0, 100}, {0., 10000.}} *)
$\endgroup$
4
$\begingroup$

I think the question is about ImageSize and not PlotRange. One way to get the "current" size

plot = Plot[x^2, {x, 0, 100}];
imgsize = 360 #/First[#] &@ (* scale to ImageSize -> Medium *)
   Rasterize[plot, "RasterSize"];
Show[plot, ImageSize -> Dynamic@imgsize]
Dynamic@imgsize

Mathematica graphics

Alternatively, one can read the cell expression of the notebook and get the value of the ImageSize option:

plot
plotcell = NextCell[];

(* after resizing *)
Options[First@First@NotebookRead[plotcell], ImageSize]
(*  {ImageSize -> {413.677, Automatic}}  *)
(* OR *)
Options[ToExpression@First@NotebookRead[plotcell], ImageSize]
(*  {ImageSize -> {413.677, Automatic}}  *)

Note how it works: If ImageSize -> Automatic or ImageSize -> 360 are settings in plot, resizing will update only the horizontal dimension. This is why both ImageSize dimensions are set in the first code. If both are set in the second code via Show[plot, ImageSize -> {360, 211}], then both dimensions will be updated when the image is resized.

$\endgroup$

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.