# Obtain PlotLabel from Plot

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"?

• Cases[plotwithLabel, _String, Infinity][[-1]] Jan 15, 2020 at 18:57

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

In:= Options[plotwithLabel, PlotLabel]

Out= {PlotLabel -> "This is Label"}

PlotLabel /. plotwithLabel[]


"This is Label"

• 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! Jan 18, 2020 at 20:51
• @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[...].
– kglr
Jan 18, 2020 at 22:42
• 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. 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 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.

• 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 Jan 15, 2020 at 20:45
• @Histoscienology I added an explanation in the answer; hope this might help. Jan 17, 2020 at 15:52

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.

• PlotLabel//Association@Options@plotwithLabel uses 2 less characters to give the same output, not to play code-golf or anything, but... Jan 18, 2020 at 20:48
• Another thing to consider would be Lookup[Options[plotwithLabel], PlotLabel] Jan 19, 2020 at 4:29
• @JasonB. Aha! Lookup is nice :) The more I learn, the more I know I know nearly nothing about WL. I feel like OptionValue[plotwithLabel[],PlotLabel] should work without having to use Part. 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:= Cases[plotwithLabel[], Rule[PlotLabel, x_] -> x] // First
Out= This is Label

In:= Cases[Unevaluated[Plot[x, {x, 1, 2}, PlotLabel -> "This is Label"]],
HoldPattern[PlotLabel -> _]]
Out= {PlotLabel -> "This is Label"}