2
$\begingroup$

I have data files which are in "text tab delimited". I have imported these files using the following function:

data = Map[Import[#, "Data"] &,FileNames["*.txt", {"directoryname"}]]

I want to use ListPlot to plot 60 functions (not at the same time), but I want to show the name of the files that I have imported in the PlotLegends so that when I plot each file, the legends automatically show the name of the file.

Here is the rest of the code:

e[t_] := data[[t]];
ListPlot[Table[e[t][[9]], {t, 1, 59, 20}], Joined -> True, PlotRange -> All,DataRange -> {1000, 1100},PlotLegends -> Automatic]   

Thanks

$\endgroup$
4
  • $\begingroup$ Edit your question to include that information, instead of leaving it in the comments. $\endgroup$ Jun 7, 2020 at 5:42
  • $\begingroup$ @saeid: Welcome to Mathematica SE. I've not totally understood what you old like to do. In your ListPlot a selection of files is chosen and their and (only) this filenames should be in the legend, right? $\endgroup$
    – mgamer
    Jun 7, 2020 at 6:23
  • $\begingroup$ @mgamer: yes that is right $\endgroup$
    – saeid
    Jun 7, 2020 at 9:35
  • $\begingroup$ @saeid: O.K. I've put this as an answer below $\endgroup$
    – mgamer
    Jun 7, 2020 at 9:54

3 Answers 3

6
$\begingroup$

Assuming that I understood your right... making my toy-data and the filenames

data = Table[RandomInteger[200, 20], {60}];
files = StringJoin["data", #, ".csv"] & /@ (ToString /@ Range[60])

export...

Export[#[[1]], #[[2]]] & /@ ({files, data}\[Transpose]);

Get the data in, similar like you did:

inFiles = FileNames["*.csv", NotebookDirectory[]];
data = Flatten /@ (Import[#] & /@ inFiles)

Taking a random sample

toPlot = RandomInteger[60, 5]

Doing the ListPlot with appropriate filenames

ListPlot[data[[toPlot]], Joined -> True, 
 PlotLegends -> (inFiles[[toPlot]])]

enter image description here

If you want only the filenames, without the directory, you have to strip of the directory, you can use something like (there are a lot of different ways):

StringCases[#, "data" ~~ x__ -> "data" ~~ x] & /@ inFiles 
$\endgroup$
3
$\begingroup$

Here is an alternative solution that uses the fact that the PlotLegends are automatically populated when the data are passed in as an Association:

Using the example data from @mgamer's answer:

data = Table[RandomInteger[200, 20], {60}];
files = StringJoin["data", #, ".csv"] & /@ (ToString /@ Range[60])

Export[#[[1]], #[[2]]] & /@ ({files, data}\[Transpose]);

inFiles = FileNames["*.csv", Directory[]];

The import is done using AssociationMap - this way, the file names are kept together with the data from the start. We use KeyMap and FileNameTake to extract only the file name without directory:

data = Flatten /@ KeyMap[FileNameTake]@AssociationMap[Import, inFiles]
(* <|"data10.csv" -> {44, 160, 52, …}, "data11.csv" -> {171, 63, 89, …}, … *)

Now a simple call to ListLinePlot generates the required plot, together with the legend:

ListLinePlot[data[[{1, 2, 3}]]]

enter image description here

Since it is an Association, you can also specify the files to plot by the keys (i.e. file names):

ListLinePlot[data[[{"data10.csv", "data11.csv", "data12.csv"}]]]
(* same output *)
$\endgroup$
2
$\begingroup$

@saeid: I would look at the documentation around LegendLabel. You have a list of files (FileNames["*.txt"]), an indexed list of data, so your final plot will look something like ListPlot[e[#][[9]], ... , LegendLabel->file[[#]]]& /@ Range[1, 59, 20]

$\endgroup$

Your Answer

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

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