1

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!

fricken
  • 13
  • 3
  • I'm sorry, should have done that earlier, thanks for your advice! – fricken May 10 '23 at 16:21
  • By the way, the `title` function creates a `text` object, you can modify its `Position` property to move it around (at least this is the case in MATLAB, I assume Octave would do it the same way). If there is no space above the plot to fit the title, you can shift the axes down using its `Position` property. – Cris Luengo May 10 '23 at 16:43
  • Thank you very much! I thought I tried this before but apparently I didn't. This worked fine for me. – fricken May 11 '23 at 12:01

2 Answers2

1

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

Joao_PS
  • 633
  • 1
  • 9
  • Thanks for your advice, but this is not working in my case because it is only possible to add text in the plotting area but not above (as far as I know!). – fricken May 10 '23 at 16:23
  • @fricken The text coordinates are in axes coordinates, but the text can be outside the axes area. For example if your axes limites are `x=[-10,10]` and `y=[-1,1]`, you can write text at `x=0` and `y=-1.5`, and it might show up above your plot with reversed y axis. – Cris Luengo May 10 '23 at 16:41
  • @fricken, try to run my code... – Joao_PS May 10 '23 at 17:37
0

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

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57