4
$\begingroup$

I want to display one axis using dollar amounts in the tickmarks (as "$500"). Searched the documentation but I cannot see AccountingForm doing that and I am stuck.

$\endgroup$
  • $\begingroup$ Ticks should see you right. This answer: mathematica.stackexchange.com/q/5276/131 should be adaptable. $\endgroup$ – Yves Klett Oct 21 '16 at 15:09
  • $\begingroup$ Not that I can tell.Ticks -> {NumberForm[{0, 500, 1000}, NumberSigns -> {"-$", "$"}], Automatic} produces an error. $\endgroup$ – Nicholas G Oct 21 '16 at 15:14
  • $\begingroup$ I used this as a workaround, but it seems embarassingly elaborate. I define a pair of values and same with the dollar sign prepended: tks = Table[{i, NumberForm[i, NumberSigns -> {"-$", "$"}]}, {i, 0, 1000, 500}] and then inside the graph Ticks -> {tks, Automatic}. $\endgroup$ – Nicholas G Oct 21 '16 at 15:19
  • $\begingroup$ Adding a working, self-contained example may help getting more help. Your fix does not seem too bad though, if you are happy with the results. $\endgroup$ – Yves Klett Oct 21 '16 at 15:25
7
$\begingroup$

I figured it out, adapting it from here:

Define a tick function using minimum and maximum:

dollarticks[min_, max_] := {#, NumberForm[#, NumberSigns -> {"-$", "$"}]} & /@ N[FindDivisions[{min, max}, 3]]

Then use that in the graph with:

Ticks -> {dollarticks, Automatic}

Quod erat demonstrandum.

Then I developed a fuller version, that drops the trailing period signifying [working precision?]. First define a function that can also be used by itself in a tick specification:

adollartick[x_] := {x,
  If[Round[x]==x,(*is the rightmost character a period, 
   from some precision?*)
   NumberForm[Round@x, NumberSigns -> {"-$", "$"}, 
      DigitBlock -> 3](*put dollar sign and comma and use Round to drop the last digit*)
   ,(*else*)
   NumberForm[x, NumberSigns -> {"-$", "$"}, 
    DigitBlock -> 3](*just put dollar sign and comma*)
   ]}

and then define the function for putting few dollar ticks:

dollarticks[min_, max_] := adollartick[#] & /@ 
  N[FindDivisions[{min, max}, 
    3]](*to produce dollar amounts as tick marks with few tickmarks*)

or many

dollarticks7[min_, max_] := 
 adollartick[#] & /@ 
  N[FindDivisions[{min, max}, 
    7]](*to produce dollar amounts as tick marks with more tickmarks*)

While we are at it, do the same for percentage ticks:

apercenttick[x_] := {x, If[
    Round[100 x] == 100 x,(*is rightmost a period?*)
    ToString[Round[100 x]],(*then drop it by using Round*)
    ToString[100 x]](*else just multily by 100*)
   <> "%"}(*and append a % sign*)

percentticks[min_, max_] := apercenttick[#]& /@ N[FindDivisions[{min, max}, 7]]
$\endgroup$

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.