0

I have a list of data called DataList. I can plot the data using ListPlot and name that plot after the input list like this:

ListPlot[DataList, PlotLabel -> ToString[HoldForm[DataList]]]

This works and I get the plot just like I want to. But I have a lot of data lists, so I write a function for this purpose, using the following:

plot[input_] := ListPlot[input, PlotLabel -> ToString[HoldForm[input]]]

However, when I input a list now, the “PlotLabel” will not reflect the name of the list I put in, but rather the content of that list.

Is there any way to extract the name of the list/expression before the function evaluates it?

Any other ideas to circumvent the problem?

I am very grateful for any tips.

Stian
  • 3
  • 1

1 Answers1

1
DataList = {1, 2, 5, 4, 3, 7};
SetAttributes[plot, HoldFirst];
plot[input_] := ListPlot[input, PlotLabel -> ToString[Unevaluated[input]]];
plot[DataList]

...PlotWithPlotLabel  DataList  snipped...
Bill
  • 3,664
  • 1
  • 12
  • 9
  • For some reason, it won't work when I map the "plot"-function on several data lists, like this: Map[plot,{DataList01,DataList02,...}] But if I also give plot the "Listable" attribute, I can at least do this: plot[{DataList01,DataList02,...}] – Stian Feb 01 '14 at 13:16
  • @Stian That's because `Map` doesn't hold its second argument, so e.g. `DataList01` will evaluate. You can prevent this with `Map[plot, Unevaluated @ {DataList01,DataList02,...}]`. I encourage you to ask any addition question on the dedicated [Mathematica.SE] site. – Mr.Wizard Feb 02 '14 at 08:52
  • Bill, +1, and I encourage you also to join us on [Mathematica.SE]. – Mr.Wizard Feb 02 '14 at 08:53