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?
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.
You can use the ResourceFunction
GraphicsInformation to obtain this information:
"ImageSize" /. ResourceFunction["GraphicsInformation"] @ Plot[x^2,{x,0,100}]
{360., 212.293}
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.}} *)
```
RegionBounds@*DiscretizeGraphics
$\endgroup$
I can suggest another simple solution:
plot = Plot[x^2, {x, 0, 100}]
Association[Options[plot]][PlotRange]
(* {{0, 100}, {0., 10000.}} *)
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
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.