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"?
PlotLabel /. plotwithLabel[[2]]
"This is Label"
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$
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$
Commented
Jan 19, 2020 at 0:19
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 Rule
s (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
.
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 Rule
s returned by Options
into an Association
from which you can simply & directly request the PlotLabel
.
PlotLabel//Association@Options@plotwithLabel
uses 2 less characters to give the same output, not to play code-golf or anything, but...
$\endgroup$
Commented
Jan 18, 2020 at 20:48
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$
Commented
Jan 19, 2020 at 7:47
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
In[2]:= Cases[Unevaluated[Plot[x, {x, 1, 2}, PlotLabel -> "This is Label"]],
HoldPattern[PlotLabel -> _]]
Out[2]= {PlotLabel -> "This is Label"}
Cases[plotwithLabel, _String, Infinity][[-1]]
$\endgroup$