# Styling ticks, axes and other elements in a Plot of a step function

To display the effect of uniform quantization (step size Q) I would like to draw a curve as shown below

I tried this using

Plot[Round[n], {n, -3, 3},
Ticks -> {{-3, -2, -1, 1, 2, 3}, {-3, -2, -1, 1, 2, 3}},
AspectRatio -> Automatic, ExclusionsStyle -> Opacity[1],
AxesLabel -> {Input, Output}]


but couldn't get it to look perfectly as shown.

As shown above tick lebels appear on different sides of the Y-axis. Can this be attained too?

• Do you have to have "+" on positive ticks ? Commented Jan 29, 2012 at 19:21
• Welcome to Mathematica.SE! Please set a descriptive title to you question. The questions here should ideally be useful to anyone who has a similar problem to solve, not just the original asker. So it is important that the titles be good summaries of the question. Commented Jan 29, 2012 at 20:14

For the arrow heads on the axes, use an Epilog inside the Plot with the Arrow function, or use the techniques described in this post: https://stackoverflow.com/questions/5844790/arrows-for-the-axes.

For the tick labeling, use for each item in the list of x-tick and y-tick locations not just the number but a list that includes the number and the corresponding label:

Ticks -> {{{-3, -3 Q}, {-2, -2 Q}, {-1, -Q}, {1, +Q}, {2, 2 Q}, {3,
3 Q}}, {-3, -2, -1, 1, 2, 3}}


where I've done it just with the x-ticks. If you insist on having the "+" prefixes on the positive ones, you could change, say, {1,+Q} to:{1, TraditionalForm@HoldForm[+Q]} and similarly for the others.

Of course you could write a little function that you would do all that with the ticks simply by applying it to the list of x-tick numbers and the list of y-tick numbers.

By default, as you've seen, tick marks are drawn only to the positive side of the axis and at a predetermined length. To change that to get each tick mark crossing the axis, use an option third entry for each tick: a list {plen,nlen} giving (as a fraction of the image size, I believe) how far the tick mark should extend in the positive and negative direction from the axis. For example:

Ticks -> {{{-3, -3 Q, {0.01, 0.01}...


You didn't say exactly what features of the displayed graphic you couldn't satisfactorily reproduce in Mathematica, but perhaps the size of the text, including tick labels, is an issue. In that case, you could use the BaseStyle-> option to Plot if you wanted to uniformly change all the text sizes, fonts, weights, etc. If, however, you want different treatment of different text elements, then you could modify each one by a Style treatment, e.g.:

Ticks -> {{{-3, -Style[3 Q, 24, Red, Bold, FontFamily -> "Papyrus"],...


(My system has that font installed; yours may not.)

• This was very informative. Thanks! Commented Jan 29, 2012 at 19:52

A function that works for any symmetric range:

plt[rng_] := Block[{r = rng, tcks, f},
f[x_] := Which[x == 1, "+Q", x == -1, "-Q", x > 0,
"+" <> ToString[x] <> "Q", x < 0, ToString[x] <> "Q", x == 0, ""];
tcks = Transpose[{Range[-r, r], f /@ Range[-r, r] }];
Plot[Round[n], {n, -r, r}, Ticks -> {tcks, tcks},
AspectRatio -> Automatic, ExclusionsStyle -> Opacity[1],
AxesLabel -> {"Input", "Output"},
AxesStyle -> Arrowheads[{-0.05, 0.05}], ImageSize -> 300]]

plt /@ {3, 5} // Row


• Actually, my solutions work for any range as well. I like the arrowheads though. Commented Jan 29, 2012 at 23:02
• @Heike Yes, you got a very nice solution. I just wanted to let Bhaskar know that in Mathematica it is easy to automate workflow. Commented Jan 30, 2012 at 1:27

I don't know what about the plot you would like to improve, but this would put a + in front of the positive tick marks and extend the ticks to the negative side of the axes

Plot[Round[n], {n, -3, 3}, AspectRatio -> Automatic,
Exclusions -> None, AxesLabel -> {Input, Output},
Ticks -> Function[{xmin, xmax},
Table[{i, Row[{NumberForm[i, NumberSigns -> {"-", "+"}], "Q"}], {.02, .02}},
{i, Floor[xmin], Ceiling[xmax]}]]
]


Slightly updated version in case you want +Q instead of +1Q

Plot[Round[n], {n, -3, 3}, AspectRatio -> Automatic,
Exclusions -> None, AxesLabel -> {Input, Output},
Ticks -> Function[{xmin, xmax},
Table[{i,
Row[{Switch[i, 1, "+", -1, "-", _, NumberForm[i, NumberSigns -> {"-", "+"}]],
"Q"}],
{.02, .02}},
{i, Floor[xmin], Ceiling[xmax]}]
]
]


Final version incorporating the comment of the OP

Plot[Round[n], {n, -3, 3}, AspectRatio -> Automatic,
Exclusions -> None, AxesLabel -> {Input, Output},
Ticks -> Function[{xmin, xmax},
Table[{i,
Row[{
Switch[i, 1, "+", -1, "-", _, NumberForm[i, NumberSigns -> {"-", "+"}]],
"Q"}], {.02, .02}},
{i, Floor[xmin], Ceiling[xmax]}]],
AxesStyle -> With[{head = {Graphics[{Polygon[{{-1, 0.5},
{0, 0}, {-1, -0.5}}]}], 0.98}},
]
]


• Addition of "AxesStyle -> {Directive[{Arrowheads[{{-0.03, 0(Xleft), {Graphics[{Polygon[{{-1, 0.5}, {0, 0}, {-1, -0.5}}]}], 0.98}}, {0.03, 1(*Xright*), {Graphics[{Polygon[{{-1, 0.5}, {0, 0}, {-1, -0.5}}]}], 0.98}}}]}], Directive[{Arrowheads[{{-0.03, 0(Ydown), {Graphics[{Polygon[{{-1, 0.5}, {0, 0}, {-1, -0.5}}]}], 0.98}}, {0.03, 1(*Yup*), {Graphics[{Polygon[{{-1, 0.5}, {0, 0}, {-1, -0.5}}]}], 0.98}}}]}]}" will make it pefect! Thank you all Commented Jan 29, 2012 at 19:40
• @R.M I've tried that but then the labels for +Q don't have a + sign in front of them. Commented Jan 29, 2012 at 19:52
• @BhaskarDey I'm glad you figured out the triangles at the end of the axes yourself. I've added a new version to my answer. Commented Jan 29, 2012 at 19:58
• Alternatively for the Ticks: Ticks -> Function[{xmin, xmax}, Table[{i, StringForm[Switch[Sign[i], 1, "+", -1, "-"] <> If[Abs[i] != 1, "1", ""] <> "Q", Abs[i]], {.02, .02}}, {i, Floor[xmin], Ceiling[xmax]}]] Commented Jan 30, 2012 at 1:21
• @J.M. Thanks, as shown in the picture tick lebels appear on different sides of the Y-axis. Can this flexibility be attained too? Commented Jan 30, 2012 at 1:29

Something like this?

data = Table[{n - 1/2, n}, {n, -3, 3}];
ticks = {#, ToString[#] <> "Q"} & /@ {-2, -1, 1, 2};
ListLinePlot[
data,
AspectRatio -> 1,
PlotRange -> {{-2.5, 2.5}, {-2.5, 2.5}},
Ticks -> {ticks, ticks},
AxesLabel -> {"Input", "Output"},
DataRange -> {-3, 3},
InterpolationOrder -> 0
]
`