2
$\begingroup$

I am using Mathematica to analyze some sales data for my company. (A copy of the available data is here.) We are trying to analyze the sales trends for each month by item.

DateListPlot works well with individual items, but I have not found a way to plot multiple data sets (i.e., different products) on one plot. We sell several thousand products, so being able to only view one product at a time is rather cumbersome. Since we didn't start selling all of our products at one time, the start dates are different for many of our items.

Can I use DateListPlot to plot multiple data sets with different start times?

(...if not, does anyone have any good alternatives?)

(Working) code:

(* This notebook is designed to find out our most popular products, both in total and in relation to time.  *)


ClearAll; (*Clear out any stale variables. *)
data = Import[
   "C:\\Users\\sales\\Documents\\Marketing\\Data\\CSV-MMA.csv"]; (* Organize \
and manipulate our data to make it simpler. *)
data = Delete[data, 1];
data = SortBy[data, data[[All, 1]]];
data = Split[data]
data = Flatten[data, 1];
data = Drop[data, {2, 4}];
data = SplitBy[data, First];

f[q_] := Sort@(Flatten@{#[[7]], #[[1]], #[[2]], #[[4]], #[[5]], #[[6]]} & /@ 
     data[[q]]);(* Here we sort our data further. After this, it should be \
ready for graphing or grid analysis. *)
multiPlot[c_] := 
 DateListPlot[c[[All, 6]], 
  DateString[c[[All, 1]][[1]], {"Month", "/", "Year"}], 
  PlotLegends -> c[[1]][[3]], PlotTheme -> "Marketing"]
q[z_] := d[f[data, z]];
d = Map[f, {1, 2, 3}];
$\endgroup$
2
$\begingroup$

DateListPlot can be used with datasets with different timestamps:

data1 = {{{2014, 10, 1}, 10}, {{2014, 10, 15}, 12}, {{2014, 10, 30}, 15}, {{2014, 12, 30}, 10}};
data2 = {{{2014, 9, 5}, 15}, {{2014, 9, 20}, 8}, {{2014, 11, 10}, 5}, {{2014, 12, 15}, 8}};

DateListPlot[{data1, data2}, Joined -> True, PlotMarkers -> {Automatic, 14}, ImageSize -> 400]

enter image description here

With OP/s data:

dt = Import["... your directory ... \\CSV-MMA.csv"];
labels = dt[[1, {7, 4, 5, 6, 2, 3, 1}]];
data = Rest[dt][[All, {7, 4, 5, 6, 2, 3}]];
data = MapAt[DateList, data, {All, 1}];

groups = GatherBy[SortBy[data, First], Last]; (* group by SKU *)

Histogram[Length /@ groups, PlotLabel -> Style["Histogram\n Series Lengths", 16, "Panel"]]

enter image description here

groups = SortBy[groups, Length];              (* sort by series length *)
longgrps = Select[groups, Length[#] >= 10 &]; (* select long series *)
ttdt = Tooltip[{#, #2},
   Column[{Row[{"Title = ", #5}],
     Row[{"SKU = ", #6}],
     Row[{"Units Ordered = ", #2}],
     Row[{"Date = ", DateString@#1}]}]] & @@@ # & /@ longgrps; (* add tooltips *)

Plot two of the series:

DateListPlot[{ttdt[[1]], ttdt[[-1]]}, Joined -> True, 
 ImageSize -> 700, PlotMarkers -> {Automatic, 14}, 
 PlotLabel -> Style[labels[[2]], 22, "Panel"], PlotRange -> All]

enter image description here

Plot all 61 series with 10 or more observations:

DateListPlot[ttdt, Joined -> True, ImageSize -> 700, 
 PlotMarkers -> {Automatic, 14}, 
 PlotLabel -> Style[labels[[2]], 22, "Panel"], 
 ClippingStyle -> Directive[{Thickness[.01], Black}]]

enter image description here


Original post:

You can use TemporalData:

series1 = {19, 16, 9, 3, 7, 2, 17, 10, 6, 12};
timestamps1 = DateRange["May 24th 2013", DatePlus["May 24th 2013", {{9, "Week"}}], "Week"];
series2 = {10, 15, Missing[], 12, 14, 5, 12, 3, 4, 5, 6, Missing[], 8, 9, 10};
timestamps2 = DateRange["April 15th 2013", DatePlus["April 15th 2013", {{14, "Week"}}], "Week"];

td = TemporalData[{series2, series1}, {{timestamps2}, {timestamps1}}];
DateListPlot[td["Paths"], Filling -> Axis, Joined -> True]

enter image description here

$\endgroup$
  • $\begingroup$ Excellent answer! My sincere thanks for your innovative and thorough deconstruction of a difficult problem. $\endgroup$ – Qu0rk Oct 19 '14 at 0:16
  • $\begingroup$ @Qu0rk, my pleasure. Thank you for the Accept and kind words. $\endgroup$ – kglr Oct 19 '14 at 1:10

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.