11
$\begingroup$

I have a very simple question that I cant find answer: how do I extract the PlotLabel from an existing plot? For example

plotwithLabel = Plot[x, {x, 1, 2}, PlotLabel -> "This is Label"]

Any way to get the text "This is Label"?

$\endgroup$
  • $\begingroup$ Cases[plotwithLabel, _String, Infinity][[-1]] $\endgroup$ – Bob Hanlon Jan 15 at 18:57
19
$\begingroup$

Plot is returning a Graphics object, with the label specified in its Options. Retrieve the options with

In[2]:= Options[plotwithLabel, PlotLabel]

Out[2]= {PlotLabel -> "This is Label"}
|improve this answer|||||
$\endgroup$
7
$\begingroup$
PlotLabel /. plotwithLabel[[2]]

"This is Label"

|improve this answer|||||
$\endgroup$
  • 1
    $\begingroup$ does the use of the 2nd slot only work because it is the second argument of plotwithLabel? Hah wow this is actually a pretty keen answer, I anticipate this is not an expected use-case or combination, but it just fits so nicely together, hah! Love it +1! $\endgroup$ – CA Trevillian Jan 18 at 20:51
  • $\begingroup$ @CATrevillian, thank you for the vote. Second Part of the the graphics object produced by Plot contains the graphics options. So we can get the options using Options@Plot[...] or using Last@Plot[...]. $\endgroup$ – kglr Jan 18 at 22:42
  • $\begingroup$ Ah right right, but is it not dependent upon ordering of the options given? I ask for this clarification because within the Options returned, I find PlotLabel near the end of the list. Though, thinking about this further, with ReplaceAll, I suppose I understand that it would only catch the portion of values returned that matches the input, PlotLabel. $\endgroup$ – CA Trevillian Jan 19 at 0:19
5
$\begingroup$
plotwithLabel = Plot[x, {x, 1, 2}, PlotLabel -> "This is Label"];
PlotLabel /. Cases[plotwithLabel, _Rule, All]

"This is Label"

If you look at the output of e.g. SequenceForm@ InputForm@ plotwithLabel, you will see the internal representation of the plot as a Graphics object. You will note that it contains many options expressed as Rules (i.e. OptionName -> optionValue, like maybe PlotRange -> All). Such expressions are actually represented as Rule[PlotRange, All] in their native form, so they have head Rule. My Cases expression extracts all possible Rule[something, something] expressions at All levels inside the plot's internal representation. It then uses them as replacement rules in a ReplaceAll expression (/.) to fish out the value of the one you are interested in, i.e. PlotLabel.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Thank you, I wish I could accept multiple answer.. I like your answer a lot, it looks very simple. But could you please explain why it works? I'm pretty lost $\endgroup$ – Histoscienology Jan 15 at 20:45
  • 1
    $\begingroup$ @Histoscienology I added an explanation in the answer; hope this might help. $\endgroup$ – MarcoB Jan 17 at 15:52
3
$\begingroup$

Inspired by @JasonB.’s answer, which, while relatively simple-looking, still returns a rule which requires further operation in order to retrieve the PlotLabel. Other answers seem confusing, though they will work on lesser versions than when Association was introduced in v10. I propose this solution which is simple, direct, and easily gives the desired output of PlotLabel in a single line.

(Association@Options@plotwithLabel)[PlotLabel]
(* “This is Label” *)

Association turns the List of Rules returned by Options into an Association from which you can simply & directly request the PlotLabel.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ PlotLabel//Association@Options@plotwithLabel uses 2 less characters to give the same output, not to play code-golf or anything, but... $\endgroup$ – CA Trevillian Jan 18 at 20:48
  • 1
    $\begingroup$ Another thing to consider would be Lookup[Options[plotwithLabel], PlotLabel] $\endgroup$ – Jason B. Jan 19 at 4:29
  • $\begingroup$ @JasonB. Aha! Lookup is nice :) The more I learn, the more I know I know nearly nothing about WL. I feel like OptionValue[plotwithLabel[[2]],PlotLabel] should work without having to use Part. $\endgroup$ – CA Trevillian Jan 19 at 7:47
2
$\begingroup$

You can use the several mechanism of filtering data. PlotLabel is an option for Plot. The options in a Plot that you have made are in the second part of plotwithlabel. That is to say, one way is

In[29]:= Cases[plotwithLabel[[2]], Rule[PlotLabel, x_] -> x] // First
Out[29]= This is Label
|improve this answer|||||
$\endgroup$
1
$\begingroup$
In[2]:= Cases[Unevaluated[Plot[x, {x, 1, 2}, PlotLabel -> "This is Label"]], 
     HoldPattern[PlotLabel -> _]]
Out[2]= {PlotLabel -> "This is Label"}
|improve this answer|||||
$\endgroup$

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.