How to determine PlotRange to include all of graphics?

前端 未结 3 715
后悔当初
后悔当初 2020-12-13 09:25

Given Graphics object, how do I determine the range of coordinates needed to include all of graphics? Basically I need something like what Show doe

3条回答
  •  情话喂你
    2020-12-13 10:30

    You can try adding a recognizable object at a known location and then see where it shows up in the exported version to provide a scale reference. I thought a vector export (SVG or EPS) would be easier to parse, but I think raster is easier after playing around a bit.

    For example, add a green rectangle covering the theoretical plot range:

    g = Graphics[{Blue, Thickness[1], CapForm["Round"], 
       Line[{{0, 0}, {1, 1}}], Green, Rectangle[{0, 0}, {1, 1}]}];
    

    enter image description here

    im = Rasterize[g, ImageSize -> 360];
    xy = Transpose[Position[ImageData[im], {0., 1., 0.}]];
    pad = Map[{Min[#1], 360 - Max[#1] } &, xy];
    Show[g, ImagePadding -> pad]
    

    The code is basically identifying where all the green pixels are. The padding in this case is {{92, 92}, {92, 92}}, but it need not be symmetrical.

提交回复
热议问题