# manipulating the X axis notation in mathematica graph [duplicate]

I plotted a graph in mathematica that displays the function

M/1480 = (4*Pi*G*r)/(3c^2)  R^3


here ,

G=6.67*10^-11
r=10^18
c=2.997*10^8


I got the graph where R in the X axis shows the notation in meters, such as 4000, 8000, 10000 etsc. how can I give an instruction to mathematica so that it appears in km so that i get 4, 8 , 10 km in X axis..

A complete instruction to plot the graph is needed.

• Try UnitConvert, e.g.. UnitConvert[Quantity[10^7,"Meters"],"Kilometers"]
– kglr
Nov 17, 2014 at 8:29
• Welcome. Please supply the code you used so far. Nov 17, 2014 at 8:43
• see also this Q/A: Number format of axes in a plot
– kglr
Nov 17, 2014 at 9:01

If your x-axis data (first column in your data matrix) is in the form 100000 m, as in the following fake data

dt1a = Sort@ Transpose[{Quantity[RandomInteger[10^4, 100], "Meter"], RandomReal[100, 100]}];


then you can use UnitConvert on the first column of your data:

dt1b = MapAt[UnitConvert[#, "Kilometer"] &, dt2, {All, 1}];


Plotting the two data sets side by side

llp1a = ListLinePlot[dt2a, ImageSize -> 300, Frame -> True];
llp1b = ListLinePlot[dt2b, ImageSize -> 300, Frame -> True];
Row[{llp2, llp2b}, Spacer[5]]


Alternatively, you can use FrameTicks or Ticks (depending on whether have a Frame in your plot) to format the tick labels as follows:

dt2 = Sort@Transpose[{RandomInteger[10^4, 100], RandomReal[100, 100]}];

llp2a = ListLinePlot[dt2, ImageSize -> 300, Frame -> True];
llp2b = ListLinePlot[dt2, ImageSize -> 300, Frame -> True,
FrameTicks -> {{Automatic,  None},
{{#, #/1000} & /@ Range[0, 10000, 1000], None}}];
llp2c = ListLinePlot[dt2, ImageSize -> 300, Frame -> True,
FrameTicks -> {{Automatic, None},
{{#, Row[{#/1000, " km"}]} & /@ Range[0, 10000, 1000],  None}}];
Row[{llp2a, llp2b, llp2c}, Spacer[5]]


Related Q/As: Number format of axes in a plot

There are several ways to change the x-ticks. In your case, the most simple solution is probably scaling the function and not touching the tick labels at all. Plot, hence, (4*\pi Gr)/(3c^2) (1000*R) with $x$ running from $x_\text{Min}/1000$ to $x_\text{Max}/1000$

You can directly manipulate what Mathematica is placing as label

plf=FullGraphics[Plot[ (4 Pi G r)/(3 c^2) R,{R,0,100000},Frame->True]];
Show[plf/.Text[x_,y__]/;IntegerQ[x] -> Text[x/1000,y],AspectRatio-> 1]


By using FullGraphics[] all plot commands a made explicit, i.e. the ticks are explicitly Line[] command and the labels are Text[] commands. The Replace[], i.e. /. looks for text which has an integer argument and replaces it by text at the same position with a label 1000 times smaller. This works in my case as y labels are floating point. Depending on your plot details you might need to modify the condition between /; and ->.

And finally you can tell Mathematica what to put as label

Plot[ (4 Pi G r)/(3 c^2) R,{R,0,100000},Frame->True,
FrameTicks->{
Table[If[Mod[i,20000]==0, {i,i/1000,{.01,0}},
{i,"",{.005,0}}],{i,0,100000,5000}],Automatic,Automatic,Automatic
}]


This puts the ticks on y (left and right) as well as on top automatically. Your x-Ticks are placed every 5000 as minor and every 20000 as major tick. You might want to change the length though, i.e. the .01 and .005 Note, if you do not use Frame it is Ticks and not FrameTicks