Skip to main content
Added explanation asked for in comments
Source Link
MarcoB
  • 67.7k
  • 18
  • 96
  • 198
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.

plotwithLabel = Plot[x, {x, 1, 2}, PlotLabel -> "This is Label"];
PlotLabel /. Cases[plotwithLabel, _Rule, All]

"This is Label"

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.

Source Link
MarcoB
  • 67.7k
  • 18
  • 96
  • 198

plotwithLabel = Plot[x, {x, 1, 2}, PlotLabel -> "This is Label"];
PlotLabel /. Cases[plotwithLabel, _Rule, All]

"This is Label"