4
$\begingroup$

I'd like to use Quantity in my graphics, but I can't figure out how to do it even for a very simple Circle.

(* draw a circle with center in (3,2) and radius 5 *)

Graphics[Circle[{3, 2}, 5]]

(* why it won't work with Quantity? *)

x = Quantity[3, "Meters"];
y = Quantity[2, "Meters"];
r = Quantity[5, "Meters"];

Graphics[Circle[{x, y}, r]]

Errors are:

1) Coordinate {Quantity[3, "Meters"], Quantity[2, "Meters"]} should be a pair of numbers, or a Scaled or Offset form.

2) Radius Quantity[5, "Meters"] should be a positive number or pair of positive numbers, or a Scaled or Offset form.

Could you help me please? Thank you, Luca-

$\endgroup$
4
$\begingroup$

Quantity itself can't be used as its own value. You need to extract the numerical value itself, like this

x = Quantity[3, "Meters"];
y = Quantity[2, "Meters"];
r = Quantity[5, "Meters"];

Graphics[Circle[{QuantityMagnitude[x, "Meters"], 
           QuantityMagnitude[y, "Meters"]}, 
           QuantityMagnitude[r, "Meters"]]
]

Mathematica graphics

You can think of Quantity as an "object" with one of its properties is its numerical value (with no units) that you can obtain from the object with the QuantityMagnitude call

$\endgroup$
  • $\begingroup$ Thank you for your help. I'm sorry I haven't enough reputation points to vote up your answer. Is Magnitude a property of Quantity objects? Is there a way to inspect properties of a give object? For instance, if I have a Point[] object, is there a way to inspect what properties I can query? Thank you again, Luca- $\endgroup$ – Luca Apr 4 '15 at 9:27
  • $\begingroup$ @Luca M is not OO Language, but uses functions. So to find what "methods" can be used on "object" Point you look at help and see all the settings and commands that can be applied to it. Also if you do ??Point you'll see the options Options[Point]={VertexColors->None,VertexNormals->None} There are also modifiers you can apply to point, such as PointSize and change its color etc... $\endgroup$ – Nasser Apr 4 '15 at 9:44
  • $\begingroup$ ok, thank you. My path to learn Mathematica seems "not so short"! $\endgroup$ – Luca Apr 4 '15 at 9:54
  • 3
    $\begingroup$ I think it would be better to extract numbers for an explicit unit, otherwise mixed units will give unexpected results. In v10 that would be something like QuantityMagnitude[x,"Meters"], for v9 you'd need an extra call to UnitConvert... $\endgroup$ – Albert Retey Apr 4 '15 at 9:57
  • $\begingroup$ @AlbertRetey good point. Since the units are all the same in this example, this will not make a difference, but will update for illustration and to remind one of this. thanks. $\endgroup$ – Nasser Apr 4 '15 at 10:05
5
$\begingroup$

Another explanation is, that Circle is a graphical primitive, not a pure mathematical function in itself, and therefore does not accept units.

When one uses a mathematical object, units can be used throughout.

Example: The circle centered on $(xc,yc)$ with radius $r$ $$(x-xc)^2+(y-yc)^2=r^2$$ can be drawn with units in Mathematica using ContourPlot:

{xc,yc,r}=Quantity[#, "Meters"]&/@{3,2,5};

ContourPlot[(x-xc)^2 + (y-yc)^2 == r^2,
  {x, Quantity[-3, "Meters"], Quantity[8, "Meters"]},
  {y, Quantity[-3, "Meters"], Quantity[8, "Meters"]}, 
  PlotRange->{{-10,10}, {-10,10}}]

output

Mathematica automatically takes care of converting units, so you can mix e.g. "Feet" and "Millimeters" etc.

$\endgroup$
  • $\begingroup$ thank you for your further explanation! Does the same problem (a graphics primitive that doesn't accept units) apply also to Arrows[], Point[] and so on? $\endgroup$ – Luca Apr 4 '15 at 9:32
  • $\begingroup$ @Luca: Yes, I think so. Just give it a try. $\endgroup$ – Jinxed Apr 4 '15 at 9:35
  • 2
    $\begingroup$ Honestly I don't think that it is worth to spend too much effort to understand/explain something like this. It simply reflects what WRI's programmers did implement, I think it would be possible to have graphics primitives accept quantities as well. If they don't but ContourPlot does then this IMHO looks somewhat inconsistent, but probably for good reasons (e.g. performance, consistency, compatibility...) and most probably has to just be accepted "as is"... $\endgroup$ – Albert Retey Apr 4 '15 at 9:52

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.