I'm trying to add a title to the legend block in a plot I've got in Octave (like this). I've had a dig through the properties that are available on the plot and have found the 'title' attribute, but when I set it there's no change on the plot.
Here's an example of what I'm trying:
a=linspace(0, 2*pi())
h=plot(sin(a))
hleg = legend("Data 1")
set(hleg, 'title', 'Data Series')
I've also tried this:
hlegtitle = get(hleg, 'title')
get(hlegtitle, 'string')
which returns Data Series
, so I'm clearly setting the property, but the title does not appear on the legend.
Is there something else that needs to be set somewhere -- is the title block being drawn off-screen, or is there a boolean or something that causes it to be hidden I've compared the various properties on hlegtitle
with those for other components of the plot, and they all seem the same.
Is there something that I'm missing, or is this just not possible?
Your code runs fine under the current development version,
which uses graphics_toolkit("qt")
by default.
With the same dev version, but graphics_toolkit("gnuplot")
,
the legend title is missing:
It is not just "off-screen" because reducing the gca
height
does not display the title.
It is still possible to set the title "by hand", with text
:
graphics_toolkit("gnuplot")
a = linspace(0, 2 * pi, 101);
h = plot(a, sin(a));
hleg = legend("Data 1")
set(hleg, "title", "Data Series")
# This is changing the gca "position"
legend("location", "northeastoutside")
# Now tweak the gca height, to provide room above the legend
gca_pos = get(gca, "position");
gca_pos(4) *= 0.9;
# The plot width has to be tweaked by hand,
# because the legend position is not reliable
gca_pos(3) = 0.66;
set(gca, "position", gca_pos)
# set text
leg_pos = get(hleg, "position")
x_pos = 1.2;
y_pos = 1.05;
leg_title = "title";
text(x_pos, y_pos, leg_title,
"units", "normalized",
"fontName", get(gcf, "DefaultAxesFontName"),
"fontSize", get(gcf, "DefaultAxesFontSize"))