4
$\begingroup$

This question already has an answer here:

I'm trying out making a CDF and I want it to print within the margins of Letter paper. There is an ImageSize option that I would like to set; some plots will be in a grid, some alone. I thought using Quantity would be good for this since I know the width of the paper. I tried:

Plot[Sin[x], {x, 0, 4 Pi}, ImageSize -> {Quantity[4, "Inches"], Automatic}]

This gives an error:

The specified setting for the option Graphics3DBoxOptions, ImageSize cannot be used.

Does anyone know what is going on here? How do I set the image size in units? What are the units of ImageSize? Can I use UnitConvert on Quantity to get to the ImageSize units? I'm using Mma 10.

Thanks,

Edmund

$\endgroup$

marked as duplicate by Michael E2, ybeltukov, m_goldberg, Teake Nutma, bobthechemist Sep 15 '14 at 16:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Possible duplicates?: (5442), (37345) $\endgroup$ – Mr.Wizard Sep 15 '14 at 11:51
  • $\begingroup$ It would be nice, though, if the Unit-based answers were also given there. $\endgroup$ – Michael E2 Sep 15 '14 at 12:29
  • $\begingroup$ @MichaelE2 If this is closed and if it is agreeable to those who answered this question I can merge the two, which will result in these answers being moved there. Some editing may be necessary. $\endgroup$ – Mr.Wizard Sep 15 '14 at 12:52
  • 1
    $\begingroup$ Those other answers don't use the built-in units support. You'd need to create variables for all of your conversions. With the unit based answer below you don't need to do this. You can built a simple function that has all of it built in. toPrinterPointsMagnitude[distance_?QuantityQ] := QuantityMagnitude@UnitConvert[distance, "PrinterPoints"]. This type of fully support solution is what I was looking for. $\endgroup$ – Edmund Sep 15 '14 at 12:53
  • $\begingroup$ @Edmund That's what I said. It seems to be better organized, though, IMO, if these approaches were collected in one place. $\endgroup$ – Michael E2 Sep 15 '14 at 12:54
5
$\begingroup$

Perhaps

inches = 72;
Plot[Sin[x], {x, 0, 4 Pi}, ImageSize -> { 4  inches, Automatic}]

enter image description here

If you have to use Quantity you can set the ImageSize converting inches to printer points:

ImageSize -> { 72 QuantityMagnitude[Quantity[4, "Inches"]], Automatic}

Update: or, better yet,

Plot[Sin[x], {x, 0, 4 Pi}, PlotStyle->Thick,
 ImageSize -> { 
   QuantityMagnitude[
    UnitConvert[Quantity[4, "Inches"], 
        "PrinterPoints" (* or "DesktopPublishingPoints" *)]], Automatic}]

enter image description here

$\endgroup$
  • $\begingroup$ Not exactly the solution I'm looking for. I'd like to use the proper units as supported by Mma. Why does 72 = 1 inch? What are the ImageSize units? $\endgroup$ – Edmund Sep 15 '14 at 11:26
  • $\begingroup$ @Edmunds, please see the update... $\endgroup$ – kglr Sep 15 '14 at 11:27
  • $\begingroup$ @kguler, Very nice. That is it! Thanks. $\endgroup$ – Edmund Sep 15 '14 at 11:30
  • 1
    $\begingroup$ I love Mathematica!!! toPrinterPointsMagnitude[Quantity[4, "Light Year"]] LOL!! $\endgroup$ – Edmund Sep 15 '14 at 11:50
  • 1
    $\begingroup$ @rhermans, ... there is "DesktopPublishingPoints" that shows up when i use ?CalculateUnits`UnitCommonSymbols`*oint*. $\endgroup$ – kglr Sep 15 '14 at 12:28
4
$\begingroup$

The ImageSize documentation, under Details reads:

"Specifications for both width and height can be any of the following: "

d d printer's points (before magnification)

72di di inches (before magnification)

So if you want 4 inches you can use:

Plot[Sin[x], {x, 0, 4 Pi}, ImageSize -> (72 4)]

for other units

toPrintPoints = QuantityMagnitude[72 UnitConvert[#, "Inches"]] &
Plot[x, {x, 0, 1}, ImageSize -> toPrintPoints[Quantity[12, "cm"]]]
$\endgroup$
  • 1
    $\begingroup$ It seems that I'm late... $\endgroup$ – rhermans Sep 15 '14 at 11:26
  • $\begingroup$ Nice solution. Thank you for taking the time to answer. I like kguler's better as I don't have to remember what 72 is. $\endgroup$ – Edmund Sep 15 '14 at 11:32
  • 1
    $\begingroup$ I melded your function into kguler's solution to create: toPrinterPointsMagnitude[x_?QuantityQ] := QuantityMagnitude@UnitConvert[x, "PrinterPoints"]. Thanks. $\endgroup$ – Edmund Sep 15 '14 at 11:42

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