0
$\begingroup$

I am new user of mathematica. I want to plot a csv file look like this:

2020-03-13 12:18:03,7.65,4.95,6.5,297.9,6.7,74.5,35.05,2.45,39.7,31.35,39.7,22.9
2020-03-13 12:28:09,9.25,5.25,6.15,30.95,5.6,47.25,19.5,2.8,49.85,34.55,34.55,29.45
2020-03-13 12:28:15,9.7,6.9,5.05,34.15,4.7,40.45,26.2,2.85,47.7,31.8,32.55,35.6

I searched some questions, and I learn ListLinePlot, then I plot the graph using:

data = Import["D:\\R1.csv"];

ListLinePlot[{data[[All, 1]], data[[All, 2]], data[[All, 3]], 
  data[[All, 4]], data[[All, 5]], data[[All, 6]], data[[All, 7]], 
  data[[All, 8]], data[[All, 9]], data[[All, 10]], data[[All, 11]], 
  data[[All, 12]]}, ImageSize -> Full, PlotLegends -> Range[1, 12]]

However, the x axis only display numbers but not time. How to make the x axis to display time?

$\endgroup$
2
$\begingroup$

The two key concepts you need for this are

  • the DateObject primitive, which allows you to turn a string with a date (which is what the first column of your data gets imported as) into a formal date object that can be recognized by Mathematica; and

  • the DateListPlot command, which takes lists whose abscissa is a date object and produces suitable plots from them.

For your data, you can then start by transforming the first row

transformedData = Transpose[Join[
   {DateObject /@ data[[All, 1]]},
   Transpose[data[[All, 2 ;;]]]
   ]]

(notice the use of the /@ syntax to encode a Map command) and then putting that into the plotting command:

DateListPlot[
 Evaluate[Table[
   transformedData[[All, {1, j + 1}]]
   , {j, 1, 12}]]
 ]

Mathematica graphics

There's fancier ways to do this which are more in line with the Mathematica way to do things, but these are not wrong and they should be relatively easy to understand for a newcomer.

$\endgroup$
  • $\begingroup$ what does ;; mean? (Transpose[data[[All, 2 ;;]]]) $\endgroup$ – fairytale Mar 13 '20 at 17:55
  • 1
    $\begingroup$ It's a shorthand for Span -- it means the range from position 2 to the end of the list. $\endgroup$ – Emilio Pisanty Mar 13 '20 at 18:04

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.