8
$\begingroup$

Graphics only supports machine precision numbers (i.e. number that can be converted to machine precision). Take for example

Graphics[Point[{0, Exp[1000.]}]]

which produces an error because Exp[1000.] is too large to be representable as a double.

What is the simplest way to deal with situations like this? Replacing all too large numbers with just some arbitrary very large machine precision number is fine, as they'll be outside the plot range anyway. I'm sure there must be some built-in function that can fix this easily.

$\endgroup$

2 Answers 2

15
$\begingroup$

I wonder whether I have understood your question correctly because I know you'll be aware of Clip

data = 
 Clip[#, {-$MaxMachineNumber, $MaxMachineNumber}] & /@ {0, Exp[1000.]}

(*
==> {0, 1.797693135*10^308}
*)

Precision /@ data

(*
==> {\[Infinity], MachinePrecision}
*)


data = RandomReal[10, {10, 2}]~Join~{{0, Exp[1000.]}};

Graphics[Point[data], PlotRange -> {0, 10}]

Mathematica graphics

data = Map[Clip[#, {-$MaxMachineNumber, $MaxMachineNumber}] &, data, 2]

(*
==> {{1.712790207, 2.900090032}, {2.659619591, 
  7.829120544}, {1.961467042, 3.28800444}, {8.391594058, 
  6.895205615}, {7.272335729, 5.320941734}, {2.663140973, 
  0.988927991}, {3.408201238, 2.47708199}, {7.951584505, 
  7.102838229}, {6.826916007, 5.639933047}, {5.307337319, 
  1.629710693}, {0, 1.797693135*10^308}}
*)

Graphics[Point[data], PlotRange -> {0, 10}]

Mathematica graphics

$\endgroup$
5
$\begingroup$

Using @Sjoerd idea of Clipping, maybe you could use too Rescale. Something simple could be a wrapper to rescale every point inside a Graphics:

rescale[things_] := 
 Module[{points = 
    Cases[things, {_?NumericQ, _?NumericQ}, ∞], minmax, 
   rescaled},
  minmax = Transpose[{Min /@ #, Max /@ #} &@Transpose[points]];
  rescaled = Clip[minmax, {-$MaxMachineNumber, $MaxMachineNumber}];
  things /. 
     {x_?NumericQ, y_?NumericQ} :>
       {Rescale[x, minmax[[1]], rescaled[[1]]], 
        Rescale[y, minmax[[2]], rescaled[[2]]]}
  ]

This is just a "draft" since points should be taken only from graphics directives with coordinates, and then filter also the Scaled or Offset coordinates.

Graphics[rescale[{... Points[data], ..., Polygon[poly], ...}]]

Anyway, you will have to adjust the aspect ratio after doing the rescale (the rescale is different for each axis).

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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