How to change the title position in Octave Plot?

471 views Asked by At

I am plotting something in Octave and it is necessary to have the x axis on the top.

But this way, the title will overlay the axis label. Is there some way to change the position of the title?

I tried to save the title as a graphics handle and change it that way, but it didn't work.

This is a code example for my plot:

x = 0:0.1:2*pi;
y = sin(x);
plot(x,y)
box off
xlabel("x")
ylabel("sin(x)")
set(gca, "XDir", "Reverse")
set(gca, "YDir", "Reverse")
set(gca, "XAxisLocation", "top")
set(gca, "YAxisLocation", "right")
t = title("Sin(x)", "fontsize", 14);

Thanks!

2

There are 2 answers

3
Joao_PS On BEST ANSWER

You can to use the text function...

x = -10:0.1:10;
plot(x, sin (x));
xlabel("x");
ylabel("sin (x)");
text(0, 1.08, "My Plot Title",
     "fontsize", 20,
     "color", [50, 230, 120]/255,
     "horizontalalignment", "center");

enter image description here

0
Tasos Papastylianou On

I find that whenever you want finer control over different elements of a plot, it's generally worth splitting things into their own axes and then playing around with the axes.

"Playing around" may involve painting things on top of another, as if working with layer on photoshop (e.g. making the top layer transparent, or turning axes visibility 'off'), or, as in this case, stack two axes one above the other in a 'grid', and draw your plot on the bottom axes, and the title on the top. E.g.

ax_plot  = axes( 'position', [0.05,0.05,0.85,0.75 ], 'units', 'normalized' );
ax_title = axes( 'position', [0.05,0.75,0.85,0.95], 'units', 'normalized', 'visible', 'off' );

x = 0:0.1:2*pi;    y = sin(x);
plot(ax_plot, x,y);    box off;    xlabel("x");    ylabel("sin(x)")

set(ax_plot, "XDir"         ,"Reverse" , "YDir"         ,"Reverse")
set(ax_plot, "XAxisLocation","top"     , "YAxisLocation","right"  )

t = title(ax_title, "Sin(x)", "fontsize", 14, 'position', [0.5,0.15] );

enter image description here