Message Boards Message Boards

Can I import and plot more than one timeseries in one file?

Posted 8 years ago

Hi,

I have a data csv file with three columns, formatted thus:

Country, Date, Value

Albania, 01/01/2005, 345.65

Albania, 01/01/2006, 453.45

etc

for about thirty countries and a date range of 2005->2011 I import OK using SemanticImport,

a=SemanticImport["/data/test.csv",{"Country","Date","Number"}]

but when I try and plot using DateListPlot(a) it errors with a message saying it cannot automatically interpret the horizontal scale values?

I have tried a two-column import and plot for one country, but I need a comparative plot of all the countries, and that seems to mean creating 30+ csv files for import and plot?

Can anyone suggest a solution, please?

POSTED BY: Andrew Lewis
3 Replies

Many thanks, Christopher, that is very clear. My incomplete neophyte understanding of the data constructs involved meant I would never have seen that. I shall give this a go first thing.

Cheers!

POSTED BY: Andrew Lewis

Dataset is new to me as well. I was a little surprised that DateListPlot was happy to ingest Dataset of GroupBy. My back up plan was to GatherBy the country.

b = {#[[All, 2 ;; -1]], #[[1, 1]]} & /@ GatherBy[Normal[Values[a]], First]

res1

This new structure { timeseries, legend} can be used two ways, PlotLegends option method:

DateListPlot[#1, PlotLegends -> #2] & @@ Transpose[b]

res2

Or the Legended method:

DateListPlot[Legended @@@ b]

res3

I make up a set up data in the form of a csv file for working with, like the one in your example:

fileText = "Country,Date,Value
  Albania,01/01/2005,345.65
  Albania,01/01/2006,453.45
  Bangladesh,01/01/2005,34.65
  Bangladesh,01/01/2006,45.45";

Then I make an 'a', using SemanitcImportString to simulate reading your file:

a = SemanticImportString[fileText, {"Country", "Date", "Number"}]

res1

What you are now working with is a Dataset:

a[All, {"Date", "Value"}]

res2

What DateListPlot wants is separate timeseries, one for each Country:

a[GroupBy["Country"], All, {"Date", "Value"}]

res3

DateListPlot[a[GroupBy["Country"], All, {"Date", "Value"}]]

res4

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract