Search code examples
plotwolfram-mathematicamathematica-8

How can I get the plot label from the name of the plotted list?


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.


Solution

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