2
$\begingroup$

I'm trying to create a discrete plot with horizontal tick labels consisting of specified text. Here's a simple example:

X={2,3,4,5}
DiscretePlot[X[[k]], {k, 1, 4}]

My labels are stored in a list:

labels={bob,ted,carol,alice}

I'd like to know what options added to the DiscretePlot command will yield a result in which the label names appear along the horizontal axis, with each name appearing under the corresponding segment in the plot. Because I'll eventually have names of longer length, I'd also like to rotate the name labels a specified amount, e.g. 45 degrees.

$\endgroup$
1

2 Answers 2

4
$\begingroup$
xx = {2, 3, 4, 5};
labels = {bob, ted, carol, alice};

DiscretePlot[xx[[k]], {k, 1, 4}, 
 PlotRange -> {{0, 5}, {0, Automatic}}, ImagePadding -> 55, 
 Ticks->{Transpose[{Range@4, Column /@ Transpose[{Range@4, Rotate[#, Pi/2]& /@ labels}]}], 
   Automatic}]

Mathematica graphics

Update: Alternatively, use Epilog or Prolog:

lp = ListPlot[List /@ Thread[{Range@Length@xx, -1/2}], 
   PlotStyle -> Directive[{Black, FontSize -> 14}], 
   PlotMarkers -> (Rotate[ToString@#, Pi/4] & /@ labels)];

DiscretePlot[xx[[k]], {k, 1, 4}, 
 PlotRange -> {{0, 5}, {0, Automatic}}, Filling -> Axis, 
 ImagePadding -> {{Automatic, Automatic}, {100, Automatic}}, 
 Epilog -> lp[[1]]]

Mathematica graphics

$\endgroup$
3
$\begingroup$

I believe this is what you're after:

X = {2, 3, 4, 5};
DiscretePlot[X[[k]], {k, 1, 4}, 
 Ticks -> {{{1, Rotate["Bob", 45]}, {2, Rotate["Ted", 45]}, {3, 
     Rotate["Carol", 45]}, {4, Rotate["Alice", 45]}}, Automatic}, 
 PlotRange -> {{0, 5}, Automatic}]

Notice the use of the Ticks option with the Rotate wrapper.

Rotated ticks

Edit: Or even something like this if you want to maintain the tick's number as well:

labels = {"bob", "ted", "carol", "alice"};
rotatedLabels = 
 MapThread[{#1, Column[{#1, Rotate[#2, Pi/2]}]} &, {Range[4], labels}]

X = {2, 3, 4, 5};
DiscretePlot[X[[k]], {k, 1, 4}, Ticks -> {rotatedLabels, Automatic}, 
 PlotRange -> {{0, 5}, Automatic}]

$\endgroup$
3
  • $\begingroup$ I think they wanted the numbers in the ticks, as well as labels below the numbers. $\endgroup$
    – Nasser
    Feb 23, 2017 at 21:38
  • $\begingroup$ @Nasser I didn't gather that originally as it wasn't made explicitly clear, but I've edited my response to hopefully accommodate the OP in case that's what they're after. Thanks! $\endgroup$
    – ktm
    Feb 23, 2017 at 21:45
  • $\begingroup$ Thank you very much. The MapThread suggestion works well, and I've been able to easily modify the idea for application barplots and matrix plots. $\endgroup$
    – fishbacp
    Feb 24, 2017 at 20:32

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.