6
$\begingroup$

I have a ListLinePlot, for which the x-axis is time (November 2011 to January 2014). Now, when I plot it, the x-axis ticks run from 1 to 28 because the time duration is 28 months.

Is there a way to the x-axis ticks labelled in months?

My code:

ListLinePlot[{list1, list2}, 
  PlotLabel -> "November 2011 to January 2014", 
  PlotLegends -> {"All Users", "Fraudsters"}, 
  AxesLabel -> {"Time", "Active Users per Month"}]
$\endgroup$
2
  • 1
    $\begingroup$ Take look at DateListPlot and TimeSeries $\endgroup$
    – Andy Ross
    Commented Jul 18, 2015 at 20:40
  • 1
    $\begingroup$ DateListPlot[{list1, list2}, {2011, 11}, DateTicksFormat -> {"MonthNameShort", " ", "YearShort"}] $\endgroup$
    – ilian
    Commented Jul 18, 2015 at 20:52

1 Answer 1

10
$\begingroup$

Taking ilian's commentary suggestion and running with it, I found I could get a nice looking plot by tweaking some options.

SeedRandom[42]; data = {RandomInteger[{2000, 5000}, 28], RandomInteger[500, 28]};
ticks = DateRange[{2011, 11}, {2014, 2}, {2, "Month"}][[All, {1, 2}]]
{{2011, 11}, {2012, 1}, {2012, 3}, {2012, 5}, {2012, 7}, {2012, 9}, {2012, 11}, 
 {2013, 1}, {2013, 3}, {2013, 5}, {2013, 7}, {2013, 9}, {2013, 11}, {2014, 1}}
DateListPlot[data, {2011, 11},
  AspectRatio -> 1/3,
  DateTicksFormat -> {"MonthShort", "/", "YearShort"},
  PlotLabel -> "November 2011 to January 2014", 
  PlotLegends -> {"All Users", "Fraudsters"}, 
  FrameLabel -> {"Date", "Active Users per Month"},
  FrameTicks -> {{Automatic, Automatic}, {ticks, Automatic}},
  ImageSize -> Large]

plot

Note the following tweaks:

  • Custom ticks computed to get date ticks at two-mont intervals
  • FrameLabel in place of AxesLabel because DatePlot produces a framed plot
  • The bottom frame label is "Date" rather than "Time"
  • Aspect ratio adjusted to be more appropriate (allows more tick labels)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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